Java Date Time Java
In this quick example, we will show how to convert java.util.Calendar to java.time.LocalDateTime.
package com.logicbig.example; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Calendar; import java.util.TimeZone; public class CalendarToLocalDateTimeExample { public static LocalDateTime toLocalDateTime(Calendar calendar) { if (calendar == null) { return null; } TimeZone tz = calendar.getTimeZone(); ZoneId zid = tz == null ? ZoneId.systemDefault() : tz.toZoneId(); return LocalDateTime.ofInstant(calendar.toInstant(), zid); } public static void main(String[] args) { Calendar now = Calendar.getInstance(); LocalDateTime localDateTime = toLocalDateTime(now); System.out.println(localDateTime); } }
2020-08-03T23:42:16.869