from(ZonedDateTime): This method is available in java.util.GregorianCalendar class of Java.
Syntax:
GregorianCalendar java.util.GregorianCalendar.from(ZonedDateTime zdt)
This method takes one argument. This method obtains an instance of GregorianCalendar with the default locale from a ZonedDateTime object.
Parameters: One parameter is required for this method.
zdt: the zoned date-time object to convert.
Returns: the gregorian calendar representing the same point on the timeline as the zoned date-time provided.
Throws:
1. NullPointerException - if zdt is null.
2. IllegalArgumentException - if the zoned date-time is too large to represent as a GregorianCalendar.
Approach 1: When no exception
Java
import java.time.ZonedDateTime;import java.util.GregorianCalendar;public class GregorianCalendarfrom {public static void main(String[] args) {ZonedDateTime zdt = ZonedDateTime.now();System.out.println(GregorianCalendar.from(zdt));}}
Output:
java.util.GregorianCalendar[time=1644077834745,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2022,MONTH=1,WEEK_OF_YEAR=5,WEEK_OF_MONTH=1,DAY_OF_MONTH=5,DAY_OF_YEAR=36,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=47,SECOND=14,MILLISECOND=745,ZONE_OFFSET=19800000,DST_OFFSET=0]
Approach 2: NullPointerException
Java
import java.time.ZonedDateTime;import java.util.GregorianCalendar;public class GregorianCalendarfrom {public static void main(String[] args) {ZonedDateTime zdt = null;System.out.println(GregorianCalendar.from(zdt));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.time.ZonedDateTime.getZone()" because "zdt" is null at java.base/java.util.GregorianCalendar.from(GregorianCalendar.java:3287)
No comments:
Post a Comment