importjava.time.Instant;importjava.time.LocalDateTime;importjava.time.OffsetDateTime;importjava.time.ZonedDateTime;publicclassMain {publicstaticvoidmain(String[] args) {// Get the current zoned date time for the system default time zoneZonedDateTimezdt1 =ZonedDateTime.now();System.out.println("Cur...
1. LocalDateTime -> ZonedDateTime To convert from LocalDateTime to ZonedDateTime, we must add the zone offset to the local date-time. Whatever the zone information we will add, the constructed object will represent an instant in the universal timeline with the configured offset. ZonedDateTime =...
新的 java.time 中包含了所有关于本地日期(LocalDate)、本地时间(LocalTime)、本地日期时间(LocalDateTime)、时区(ZonedDateTime)和持续时间(Duration)的类。历史悠久的 Date 类新增了 toInstant() 方法,用于把 Date 转换成新的表示形式。这些新增的本地化时间日期 API 大大简化了日期时间和本地化的管理。 java....
将LocalDateTime与默认时区(或指定时区)结合,转换为ZonedDateTime。 将ZonedDateTime转换为Instant。 使用Date.from(Instant instant)方法将Instant转换为Date。 3. 编写代码实现java.time.LocalDateTime到java.util.Date的转换 以下是具体的代码示例: java import java.time.LocalDateTime; import java.time.ZoneId; impor...
Convert the JapanZonedDateTimeUTC+9 back to aInstantUTC+0/Z, review the result, the offset is taken care automatically. InstantZonedDateTime2.java packagecom.mkyong.date;importjava.time.*;publicclassInstantZonedDateTime2{publicstaticvoidmain(String[] argv){LocalDateTimedateTime=LocalDateTime.of(2016...
import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; public final class JDJDateUtils { public static Date convertToDate(LocalDateTime date) { return Date.from(date.atZone(ZoneId.systemDefault()) .toInstant()); ...
Java ZonedDateTime convert to GregorianCalendar Copy importjava.time.ZonedDateTime;importjava.util.GregorianCalendar;publicclassMain {publicstaticvoidmain(String[] args) {ZonedDateTimezdt =ZonedDateTime.now();System.out.println("Zoned DateTime: "+ zdt);//www.java2s.comGregorianCalendargc2 =GregorianCa...
System.out.println("ZonedDateTime : "+ jpTime); System.out.println("OffSet : "+ jpTime.getOffset()); } } Output Instant : 2016-08-18T06:17:10.225Z LocalDateTime : 2016-08-18T06:17:10.225 2. ZonedDateTime -> Instant Convert the JapanZonedDateTimeUTC+9 back to aInstantUTC+0/Z, ...
Learn to convert a specified Instant (in UTC) to ZonedDateTime (at some zone), and ZonedDateTime to Instant with easy-to-follow Java examples.
1. ZonedDateTime Always use this new Java 8java.time.ZonedDateTimeto represent a date and time containing time zone. ZonedDateTimeExample.java packagecom.mkyong.date;importjava.time.LocalDateTime;importjava.time.ZoneId;importjava.time.ZonedDateTime;importjava.time.format.DateTimeFormatter;publicclass...