提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将GregorianCalendar转换为新的Datetime类型,反之亦然
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; /* nowjava */ import java.time.OffsetDateTime; import java.time.OffsetTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.GregorianCalendar; import java.util.TimeZone; public class Main { public static void main(String[] args) { // Create a GC for the default time zone /** from 时 代 J a v a - nowjava.com**/ GregorianCalendar gc = new GregorianCalendar(2017, 1, 11, 15, 45, 50); System.out.println("Gregorian Calendar: " + gc.getTime()); // Convert the GC to a LocalDate LocalDate ld = gc.toZonedDateTime().toLocalDate(); System.out.println("Local Date: " + ld); // Convert the GC to a LocalTime LocalTime lt = gc.toZonedDateTime().toLocalTime(); System.out.println("Local Time: " + lt); // Convert the GC to a LocalDateTime LocalDateTime ldt = gc.toZonedDateTime().toLocalDateTime(); System.out.println("Local DateTime: " + ldt); // Convert the GC to an OffsetDate OffsetDateTime od = gc.toZonedDateTime().toOffsetDateTime(); System.out.println("Offset Date: " + od); // Convert the GC to an OffsetTime OffsetTime ot = gc.toZonedDateTime().toOffsetDateTime().toOffsetTime(); System.out.println("Offset Time: " + ot); // Convert the GC to an ZonedDateTime ZonedDateTime zdt = gc.toZonedDateTime(); System.out.println("Zoned DateTime: " + zdt); // Convert the ZonedDateTime to a GC. In GC month starts at 0 // and in new API at 1