`
piperzero
  • 浏览: 3474111 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

简化Java日期操作的开源项目DATE4J

 
阅读更多

来源http://www.oschina.net/question/8676_8449

以往要使用Java对时间日期进行操作,可能会用到以下的一些类:

Date and its subclasses :

The calendar and time zone classes :

The formatting and parsing classes :

以上这么多类用起来是不是很麻烦呢,现在好了,有了DATE4J,就不用这么麻烦了。

下面是一些简单的实例:

  1. //Examples
  2. //Here are some quick examples of using date4j's DateTime class :
  3. DateTime dateAndTime = new DateTime("2010-01-19 23:59:59");
  4. DateTime dateAndTime = new DateTime("2010-01-19 23:59:59.123456789");
  5. DateTime dateOnly = new DateTime("2010-01-19");
  6. DateTime timeOnly = new DateTime("23:59:59");
  7. DateTime dateOnly = DateTime.forDateOnly(2010,01,19);
  8. DateTime timeOnly = DateTime.forTimeOnly(23,59,59,0);
  9. DateTime dt = new DateTime("2010-01-15 13:59:15");
  10. boolean leap = dt.isLeapYear(); //false
  11. dt.getNumDaysInMonth(); //31
  12. dt.getStartOfMonth(); //2010-01-01, 00:00:00.000000000
  13. dt.getEndOfDay(); //2010-01-15, 23:59:59.999999999
  14. dt.format("YYYY-MM-DD"); //formats as '2010-01-15'
  15. dt.plusDays(30); //30 days after Jan 15
  16. dt.numDaysFrom(someDate); //returns an int
  17. dueDate.lt(someDate); //less-than
  18. dueDate.lteq(someDate); //less-than-or-equal-to
  19. //Although DateTime carries no TimeZone information internally, there are methods that take a TimeZone as a parameter :
  20. DateTime now = DateTime.now(someTimeZone);
  21. DateTime today = DateTime.today(someTimeZone);
  22. DateTime fromMilliseconds = DateTime.forInstant(31313121L, someTimeZone);
  23. birthday.isInFuture(someTimeZone);
  24. dt.changeTimeZone(fromOneTimeZone, toAnotherTimeZone);

获取DATE4J.jar请访问DATE4J项目网站:http://www.date4j.net

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics