几天都没写blog了,最近事有点多,书看的也不是很多。其实与日期相关的类这一部分早就看完了,但一直没有时间写。今天补上。:)
首先是格式化日期,Jakarta中可以使用DateFormatUtils、FastDateFormat。这两个类的区别是如果你自定义显示的格式就使用FastDateFormat,否则可以使用DateFormatUtils。DateFormatUtils的使用方法将下面的代码:
public static void main(String[] args) {
// TODO Auto-generated method stub
Date now = new Date();
String isoDT = DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(now);
System.out.println("It is currently: " + isoDT);
}
DateFormatUtils中主要使用到以下几个FastDateFormat属性。
FastDateFormat类则可以按照要求进行显示,所用到的方法是在getInstance()中进行设置。
static FastDateFormat getInstance()
static FastDateFormat getInstance(String pattern)
static FastDateFormat getInstance(String pattern, Locale locale)
static FastDateFormat getInstance(String pattern, TimeZone timeZone)
static FastDateFormat getInstance(String pattern, TimeZone timeZone, Locale locale)
通过这五个方法,就可以构造出适合的格式化时间了。
有时我们并不需要精确的时间,而是需要一个大概的时间,那么我们就需要舍入Date对象,我们得以使用的工具是DateUtils。通过DateUtils与Calendar的配合,就可以达到舍入对象的目的。我们使用round()方法来舍入Date对象。
static Calendar round(Calendar date, int field)
static Date round(Date date, int field)
static Date round(Object date, int field)
我们调用这三个方法中的一个就可以轻松的在java中实现Date对象的舍入操作。与舍入相关的还有一个方法——truncate(),该方法可以截取Date对象。
今天就写到这里,该睡觉了。呵呵。
首先是格式化日期,Jakarta中可以使用DateFormatUtils、FastDateFormat。这两个类的区别是如果你自定义显示的格式就使用FastDateFormat,否则可以使用DateFormatUtils。DateFormatUtils的使用方法将下面的代码:







ISO_DATE_FORMAT | ISO8601格式,但不显示时区 |
ISO_DATE_TIME_ZONE_FORMAT | 类似ISO8601格式,但是显示时区 |
ISO_DATETIME_FORMAT | ISO8601格式,不显示时区 |
ISO_DATETIME_TIME_ZONE_FORMAT | ISO8601格式,显示时区 |
ISO_TIME_FORMAT | ISO8601格式,不显示时区 |
ISO_TIME_NO_T_FORMAT | 类似ISO8601格式,不显示时区 |
ISO_TIME_NO_T_TIME_ZONE_FORMAT | 类似ISO8601格式,显示时区 |
ISO_TIME_TIME_ZONE_FORMAT | ISO8601格式,显示时区 |
STMP_DATETIME_FORMAT | SMTP数据头 |
FastDateFormat类则可以按照要求进行显示,所用到的方法是在getInstance()中进行设置。





有时我们并不需要精确的时间,而是需要一个大概的时间,那么我们就需要舍入Date对象,我们得以使用的工具是DateUtils。通过DateUtils与Calendar的配合,就可以达到舍入对象的目的。我们使用round()方法来舍入Date对象。



今天就写到这里,该睡觉了。呵呵。