この新しいDate and Time API(JSR-310)を使うか,Joda Timeを使い続けるかはあなた次第だが,少なくとも旧来のDateの実装で無理をし続ける必要だけはなさそうである.
ちなみに以下のサイトは良質なDate and Time APIのサマリ,Joda TimeとJSR-310の比較記事だと思う.
http://sssslide.com/www.slideshare.net/JustinSDK/2013-java-developer-day-joda-timejsr310
http://howtodoinjava.com/2013/05/15/date-and-time-api-changes-in-java-8-lambda/
LocalDate, ZoneDateTimeそしてDateTimeFormatterの使用例
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
/**
* Created by morinoko on 12/29/13.
*/
public class Kerorin {
public static void main( String args[] ) {
LocalDate date = LocalDate.now();
System.out.println( "today:" + date );
System.out.println( "after five days from today:" + date.plusDays(5) );
//ZoneId.getAvailableZoneIds().forEach(x -> System.out.println(x));
ZonedDateTime zTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
DateTimeFormatter fmt = new DateTimeFormatterBuilder().append( DateTimeFormatter.ISO_DATE )
.appendLiteral("-")
.appendZoneId().toFormatter();
System.out.println( fmt.format( zTime ) );
System.out.println( "Converting to JST" );
System.out.println( fmt.format( zTime.withZoneSameLocal( ZoneId.of( "Asia/Tokyo" ) ) ) );
}
}
0 件のコメント:
コメントを投稿