这种日期时间工作更容易使用:
- Joda-Time 2.3 库
- 与 Java 捆绑在一起的 java.util.Date 和 .Calendar 类的流行替代品
- 开源
- 免费的
- 积极维护(截至 2014-02)
- java.time包
示例代码
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); // Or, DateTimeZone.UTC
DateTime dateTime = new DateTime( timeZone );
DateTime monthAgo = dateTime.plusMonths( -1 ); // Smartly handles various month lengths, leap year, and so on.
DateTime monthLater = dateTime.plusMonths( 1 );
转储到控制台...</p>
System.out.println( "dateTime: " + dateTime );
System.out.println( "monthAgo: " + monthAgo );
System.out.println( "monthAgo start of day: " + monthAgo.withTimeAtStartOfDay() );
System.out.println( "monthLater: " + monthLater );
运行时……</p>
dateTime: 2014-02-24T01:53:22.386+01:00
monthAgo: 2014-01-24T01:53:22.386+01:00
monthAgo start of day: 2014-01-24T00:00:00.000+01:00
monthLater: 2014-03-24T01:53:22.386+01:00