使用以下任一方法,这种日期时间工作更容易:
与Java捆绑在一起的java.util.Date和.Calendar类的热门替代品
开源
免费
积极维护(截至2014-02)
灵感来自Joda-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 );
转储到控制台......
System.out.println( "dateTime: " + dateTime );
System.out.println( "monthAgo: " + monthAgo );
System.out.println( "monthAgo start of day: " + monthAgo.withTimeAtStartOfDay() );
System.out.println( "monthLater: " + monthLater );
跑的时候......
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