您的位置:首页 > 编程语言 > Java开发

Java中关于日期类那些方法

2014-12-17 09:16 351 查看
转载请注明出处/article/8093734.html,谢谢

本篇文章主要介绍Date,DateFormat,SimpleDateFormat,Calendar这四个类

Date类:

在java.util包下

类 Date 表示特定的瞬间,精确到毫秒。

从 JDK 1.1 开始,应该使用 Calendar 类实现日期和时间字段之间转换,使用 DateFormat 类来格式化和分析日期字符串。Date 中的相应方法已废弃。

所以Date主要用来生成时间,

Date有两个构造方法

Date()

分配
Date
对象并初始化此对象,以表示分配它的时间(精确到毫秒)。


Date(long date)

分配
Date
对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。

Date date = new Date();
Date date2=new Date(0);
System.out.println(date);
System.out.println(date2);


输出结果是:

Tue Dec 16 19:40:32 CST 2014
Thu Jan 01 08:00:00 CST 1970


可以看出两个构造函数的区别,这里我个人认为第二个构造函数,用处太少,因为知道一个时间相对于“历元”的毫秒数,是不现实的。

Date常用方法:

boolean after(Date when) 测试此日期是否在指定日期之后

boolean before(Date when) 测试此日期是否在指定日期之前

int compareTo(Date anotherDate) 比较两个日期的顺序

long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数

void setTime(long time) 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点

自我感觉这几个方法都没什么意义,所以就不介绍了。

DateFormat类:

在java.text包下

DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。

它是抽象类,所以不能构造方法来实例化,可以用getDateInstance()和getDateTimeInstance()这两个静态函数来进行实例化。这两个的区别是一个返回的是日期,一个返回的是日期+时间。

同时,getDateInstance(int style, Locale aLocale)

关于style值:

FULL: 长度最长 比如:2013年1月9日 星期三

LONG: 长度更长 比如:January 9, 2013

MEDIUM:长度比SHORT长 比如:Jan 9,2013

SHORT: 完全为数字,比如:13/1/9

说了这么多,赶紧看例子吧:

DateFormat df = DateFormat.getDateInstance();
// 使用getDateTimeInstance生成format实例,就可以把date和time都显示出来
DateFormat df2 = DateFormat.getDateTimeInstance();

String s = df.format(date);
String s2 = df2.format(date);
System.out.println(s);
System.out.println(s2);
输出结果为

2014-12-16
2014-12-16 20:01:05


可以看出一个有日期,有时间,一个只有日期。

DateFormat df3 = DateFormat.getDateInstance(DateFormat.FULL,Locale.CHINA);
String china_time = df3.format(new Date());
DateFormat df4 = DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA);
String g_time = df4.format(new Date());
System.out.println("full值:" + china_time);
System.out.println("long值:" + g_time);


输出值为:

full值:2014年12月16日 星期二
long值:2014年12月16日


这里就不在解释了。

SimpleDateFormat类:

在java.text包下

它是DateFormat类的直接子类,继承DateFormat类。我是这么理解SimpleDateFormat类的,它相对于Datef类更接地气,你可以随意给他指定一个形式的日期,进行更改。

SimpleDateFormat类主要功能是完成日期之间格式的转换,而且在转换过程中需要采用如下步骤:

1.指定一个模板,并根据这个模板,取出第一个所有的时间数字。

2.所有的时间数字将采用Date类保存。

3.将所有的时间数字重新进行格式转换。

模板如下表,注意区分大小写

日期模板描述
Y表示年:yyyy
M表示月:MM
d表示日:dd
HH表示时:HH
mm表示分:mm
ss表示秒:ss
毫秒S毫秒:SSS
Date date2 = new Date();

SimpleDateFormat spf = new SimpleDateFormat("yyyy年-MM月-dd日:HH时-mm分-ss秒");

System.out.println(spf.format(date2));
输出为:

2014年-12月-16日:20时-16分-49秒


其实这里可以用sdf.format(System.currentTimeMillis()),而不是用Date类,会更快一些。

这里有两个方法,一定要搞清楚,一个是spf.format(Date),一个是spf.parse(String),分别代表,将日期变为新的格式,从给定的字符串中提取出日期,提取出来的日期Date类型。

同时在调用parse这个方法时,会出现异常,

try {

} catch (ParseException e) {

e.printStackTrace();

}

这应该不难理解,可能字符串提取不出日期,就会捕捉异常。

Calendar类:

在java.util包下

Calendar 类是一个抽象类,

它为“特定瞬间”与一组诸如 “YEAR”、“MONTH”、“DAY_OF_MONTH”、“HOUR ”等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

Calendar实例化有两种方式,第一种是 Calendar nowTime = new GregorianCalendar();,第二种是Calendar calendar=Calendar.getInstance();

看例子:

Calendar nowTime = new GregorianCalendar();// 获取当前系统平台下默认的日期、时间和时区。
int todayYear = nowTime.get(Calendar.YEAR);// 获取当前系统平台下默认日期的“年”。
int todayMonth = nowTime.get(Calendar.MONTH) + 1;// 获取当前系统平台下默认日期的“月”。
int todayDay = nowTime.get(Calendar.DAY_OF_MONTH); // 获取当前系统平台下默认日期的“日”。
// 输出当前系统平台下的时间。
System.out.println("现在的日期是:" + todayYear + "年" + todayMonth + "月"+ todayDay + "日");

nowTime.set(2013, 2 - 1, 9);
System.out.println(nowTime.getTime());

Calendar calendar3 = Calendar.getInstance();
calendar3.add(Calendar.DATE, 10);
int year1 = calendar3.get(Calendar.YEAR);// 月份
int month1 = calendar3.get(Calendar.MONTH) + 1;// 日期

int date1 = calendar3.get(Calendar.DATE);
System.out.println(year1 + "年" + month1 + "月" + date1 + "日");


输出为:

现在的日期是:2014年12月16日
Sat Feb 09 20:29:30 CST 2013
2014年12月26日


呼。。。终于完毕了。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: