JAVA日期时间处理
来源:互联网 发布:神奇百货ceo骗局 知乎 编辑:程序博客网 时间:2024/04/29 20:44
一、有关时间日期的几个问题
- UTC/UT
世界标准时间 GMT
格林尼治时间How to get the current time
System.currentTimeMillis —– 返回当前时间点的毫秒表示,一般用于记录程序的运行时间
Date date = new Date();
Calendar calendar = Calendar.getInstance();- How to format it
日期的转换使用DateForma抽象类的子类
SimpleDateFormat new SimpleDateFormat(“日期格式”) - How to convert a string to a date/time
方法一:
①、已知str=”2016,02,21”,
new SimpleDateFormat(“yyyy,MM,dd”) ———创建要解析字符串为日期的格式
②、sdf.parse(str) —–返回字符串对应的Date,SimpleDateFormat除了格式化Date为字符串外,还能把字符串按格式解析为Date方法二
①、先将字符串转为
②、调用Timestamp t =
new Timestamp().valueOf(str);- How to process the date/time with database
用java.util.Date的三个子类java.sql.Date/
java.sql.Time/java.sql.Timestamp处理
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); //获取东八区时间int year = c.get(Calendar.year); //获取年int month = c.get(Calendar.month)+1 //获取月,0表示1月份int day = c.get(Calendar.DAY_OF_MONTH); //获取当前天数int first = c.getActualMinimum(c.DAY_OF_MONTH); //获取本月最小天数int last = c.getActualMaximum(c.DAY_OF_MONTH); //获取本月最大天数int time = c.get(Calendar.HOUR_OF_DAY); //获取当前小时int min = c.get(Calendar.MINUTE); //获取当前分钟int second = c.get(Calendar.SECOND); //获取当前秒SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String curDate = s.format(c.getTime()); //当前日期
二、与时间日期处理有关的类
Date类
java.util.Date的API简介
The class Date represents a specific instant in time, with millisecond precision
它提供了很多方法,但是有些已经废弃,不再使用了,具体可以查看API文档。
下面看一个Date类的实例:
public class TestDate{ public static void main(String[] args){ TestDate testdate = new TestDate(); testdate.getSystemCurrentTime(); testdate.getCurrentDate(); } public void getSystemCurrentTime(){ System.out.println("---获取系统当前时间---"); System.out.println(System.currentTimeMils()); } public void getCurrentDate(){ System.out.println("---获取系统当前时间---"); Date date = new Date(); System.out.println("现在的日期是="+date.toString()); System.out.println("自1970年1月1日0时0分0秒开始至今所经历的毫秒数="+date.getTime()); }}
Calendar类
Calendar类API简介:
The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH,
DAY_OF_MONTH,HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
它可以完全取代Date和DateFormat的组合对日期格式的处理,并且对日期的加减操作也特别方便。
DateFormat类
API简介:
DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such as SimpleDateFormat, allows for formatting (i.e., date -> text), parsing (text -> date), and normalization. The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.
使用最多的是SimpleDateFormat,它提供了许多方法用来将Date对象转成指定风格式的字符串形式,可以将符合要求的日期格式字符串形式转换成Date对象。
三、实例
日期格式字符串到日期类对象
String datestr = new "2016年2月22日";SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm")Date date = sdf(datestr);
日期加减处理
//Calendar的add方法有两个参数,第一个指定加减的单位,第二个指定加减的数量Calendar c = getCalendar();c.add(Calendar.YEAR,-1) ;c.add(Calendar.MINUTE,-30);
Date和Calendar的转换
Calendar c = getCalendar(); Date date = new Date(c.getTimeInMillis());
四、总结
JAVA中日期经常使用一下几个方面:
①、创建日期
②、日期格式化显示
③、日期的转换(主要是和字符串之间的相互转换)
④、日期中年、月、日、时、分、秒等获取
⑤、日期的大小比较,日期的加减
- Java日期时间处理
- Java日期时间处理
- java日期时间处理
- java时间日期处理
- java日期时间处理
- Java日期时间处理
- java 时间日期处理
- java处理日期时间
- Java日期时间处理
- JAVA日期时间处理
- Java日期时间处理
- Java日期时间处理
- java处理日期时间
- java处理日期时间
- Java日期时间处理
- java Date 时间 日期 处理
- Java中的时间日期处理
- Java DateTime 日期时间处理
- iphone,ipad,android图片尺寸
- 46条for_each循环优于传统for循环
- H5小游戏定制开发,微信游戏定制开发公司 -找向阳花科技!
- Android事件分发详解(七)——将事件分发至特定的子View.
- 【Java学习笔记】集合
- JAVA日期时间处理
- Android大牛的Blog汇总
- 自己动手丰衣足食之气泡框和常见css图形(三角)
- oracle之连接实战
- hdu1025 Constructing Roads In JGShining's Kingdom(最长递增公共子序列二分法)
- 为什么C++还是无法取代C?
- Shell备份数据库脚本编写
- Shadow SSDT是什么玩意
- IOS UIPickView+sqlite 选择中国所有城市案例