1. Overview
In this article, You'll learn how to convert Calendar to LocalDateTime object in java 8 DateTime api.
LocalDateTime api can be used completely replacement to the Date class as all features of Date functions are done in a simple and precise way such as adding minutes to the current date.
2. Example Program To Convert Calendar to LocalDateTime
To convert to LocalDateTime, you need to do the following steps.
Step 1:
First, Create a Calendar instance using Calendar.getInstance() method that returns Calendar object.
Step 2 :
Next, Get the time zone from the calendar instance using the calendar.getTimeZone() method.
Step 3:
Now, Get the ZoneId object from TimeZone class using timezone.toZoneId() method.
Step 4:
Finally, Invoke the LocalDateTime.ofInstant(calendar.toInstant(), zoneId) method with the calendar instant and zone id value.
package com.javaprogramto.java8.dates.conversion; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Calendar; import java.util.TimeZone; public class CalenderToLocalDateTimeExample { public static void main(String[] args) { //Create a calender instance. Calendar calendar = Calendar.getInstance(); System.out.println("Calender instance : "+calendar); // Getting the timezone TimeZone tz = calendar.getTimeZone(); // Getting zone id ZoneId zoneId = tz.toZoneId(); // conversion LocalDateTime localDateTime = LocalDateTime.ofInstant(calendar.toInstant(), zoneId); System.out.println("Local date time object : "+localDateTime); } }
Calender instance : java.util.GregorianCalendar[time=1596972628333,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=7,WEEK_OF_YEAR=33,WEEK_OF_MONTH=3,DAY_OF_MONTH=9,DAY_OF_YEAR=222,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=0,SECOND=28,MILLISECOND=333,ZONE_OFFSET=19800000,DST_OFFSET=0] Local date time object : 2020-08-09T17:00:28.3333. Convert Calendar to LocalDate
package com.javaprogramto.java8.dates.conversion; import java.time.LocalDate; import java.time.ZoneId; import java.util.Calendar; import java.util.Date; public class CalenderToLocalDateExample { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); Date input = cal.getTime(); LocalDate la = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println("Calender to LocalDate : "+la); } }Output:
Calender to LocalDate : 2020-08-09
4. Single Line Conversion from Calendar to LocalDateTime and LocalDate in Java 8
package com.javaprogramto.java8.dates.conversion; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Calendar; public class SingleLineCalenderToLocalDateTimeExample { public static void main(String[] args) { //Create a calender instance. Calendar calendar = Calendar.getInstance(); LocalDateTime singleLineLocalDateTime = getLocalDateTime(calendar); LocalDate singleLineLocalDate = getLocalDate(calendar); System.out.println("Converted LocalDateTime : "+singleLineLocalDateTime); System.out.println("Converted LocalDate : "+singleLineLocalDate); } private static LocalDateTime getLocalDateTime(Calendar calendar){ return LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()); } private static LocalDate getLocalDate(Calendar calendar){ return LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()).toLocalDate(); } }Output:
Converted LocalDateTime : 2020-08-09T20:23:47.780
Converted LocalDate : 2020-08-09
No comments:
Post a Comment
Please do not add any spam links in the comments section.