java.util.Date对象表示一个精确到毫秒的瞬间; 但由于Date从JDK1.0起就开始存在了,历史悠久,而且功能强大(既包含日期,也包含时间),所以他的大部分构造器/方法都已Deprecated,因此就不再推荐使用(如果贸然使用的话,可能会出现性能/安全方面的问题);下面我仅介绍它还剩下的为数不多的几个方法(这些方法的共同点是Date与毫秒值的转换):
构造器
Date(): 在底层调用System.currentTimeMillis()作为日期参数.
Date(long date): 根据指定的long整数(从1970-1-1 00:00:00以来经过的毫秒数)来生成Date对象.
方法
boolean after(Date when): 测试this日期是否在指定日期when之后;
boolean before(Date when): 测试this日期是否在指定日期when之前;
long getTime(): 获取从1979-01-01 00:00:00 到Date对象之间经过的毫秒值;
void setTime(long time): 设置时间,time含义上同.
/**
* Created by jifang on 15/12/30.
*/
public class DateTest {
@Test
public void test() {
Date dateBefore = new Date();
Date dateAfter = new Date(System.currentTimeMillis() + 1);
System.out.println("before: " + dateBefore.getTime());
System.out.println("after: " + dateAfter.getTime());
System.out.println(dateBefore.before(dateAfter));
System.out.println(dateAfter.after(dateBefore));
dateBefore.setTime(System.currentTimeMillis());
System.out.println(dateBefore.getTime());
System.out.println(dateBefore.before(dateAfter));
}
}
日期格式化
完成字符串与日期对象的转化(format/parse)
java.text.DateFormat是一个抽象类, 他提供了如下几个方法获取DateFormat对象.
方法
描述
static DateFormat getDateInstance()
Gets the date formatter with the default formatting style for the default locale.
static DateFormat getDateTimeInstance()
Gets the date/time formatter with the default formatting style for the default locale.
static DateFormat getTimeInstance()
Gets the time formatter with the default formatting style for the default locale.
其实上面三个方法还可以指定日期/时间的样式, 如FULL/LONG/MEDIUM/SHOT, 通过这四个样式参数可以控制生成的格式化字符串. 但由于在我们的实际开发中很少直接用DateFormat类,因此就不对其做过多的介绍.而我们比较常用的是其子类SimpleDateFormat(其实上面几个getXxxInstance方法返回的也是SimpleDateFormat实例)
DateFormat dateFormat = DateFormat.getTimeInstance();
System.out.println(dateFormat.getClass().getName());
SimpleDateFormat
java.text.SimpleDateFormat可以非常灵活的格式化Date, 也可以用于解析各种格式的日期字符串.创建SimpleDateFormat对象时需要传入一个pattern字符串,这个pattern不是正则表达式,而是一个日期模板字符串.
/**
* Created by jifang on 15/12/30.
*/
public class FormatTest {
@Test
public void client() throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// Date -> String
Date date = new Date(System.currentTimeMillis());
System.out.println(format.format(date));
// String -> Date
String timeString = "2015-12-30 08:53:21";
Date newDate = format.parse(timeString);
System.out.println(newDate);
}
}
在时间日期格式化时, 有下面几个方法是最常用的:
方法
描述
小结
String format(Date date)
Formats a Date into a date/time string.
Date -> String
Date parse(String source)
Parses text from the beginning of the given string to produce a date.
String -> Date
当然, pattern我们还可以根据我们的需求有其他的定制形式:
@Test
public void client() throws ParseException {
DateFormat format = new SimpleDateFormat("yy年MM月dd日 hh时mm分ss秒");
// Date -> String
Date date = new Date(System.currentTimeMillis());
System.out.println(format.format(date));
// String -> Date
String timeString = "15年12月30日 09时00分29秒";
Date newDate = format.parse(timeString);
System.out.println(newDate);
}
可以看出SimpleDateFormat把日期格式化成怎样的字符串以及能把怎样的字符串解析成Date, 完全取决于创建对象时指定的pattern参数,其他的pattern参数以及SimpleDateFormat的方法可以参考JDK文档.
发表评论
评论已关闭。