try { //make sure date and time input is a valid integer
System.out.print('Year: ')
year = sc.nextInt()
System.out.print('Month: ')
month = sc.nextInt()// 1 is January
System.out.print('Day: ')
day = sc.nextInt()
System.out.print('Hour: ')
hour = sc.nextInt()
System.out.print('Minute: ')
minute = sc.nextInt()
System.out.print('Second: ')
second = sc.nextInt()
System.out.print('Millisecond: ')
millisecond = sc.nextInt()
System.out.print('Microsecond: ')
microsecond = sc.nextInt()
System.out.print('Nanosecond: ')
nanosecond = sc.nextInt()
LocalDateTime enteredDateAndTime =LocalDateTime.of(year, month, day, hour, minute, second).withNano(millisecond *1000000+ microsecond *1000+ nanosecond) // Convert datetime of GregorianCalendar object to LocalDateTime object named enteredDateAndTime without a time zone and set nanoseconds to 1,000,000 (1 million) * milliseconds + 1,000 (1 thousand) * microseconds + nanoseconds
ZonedDateTime enteredDateAndTimeTimezone =ZonedDateTime.of(enteredDateAndTime, ZoneId.systemDefault())// Get time zone of entered date and time values
// Print the entered date and time values in all supported formats
System.out.println('\nEntered Date and Time Values:\n')
System.out.println('Era: '+ era1.format(enteredDateAndTime) +' ('+ era2.format(enteredDateAndTime) +')'+' ('+ era3.format(enteredDateAndTime) +')') // Print the date and time values with formatted date and time pattern
System.out.println('Localized Time Zone Offset: '+ timezoneOffset.format(enteredDateAndTimeTimezone) +' ('+ timezoneOffset2.format(enteredDateAndTimeTimezone) +')')
System.out.println('\nCurrent Date and Time Values:\n')
}
catch (InputMismatchException ex) { //if entered, date and time values are in the wrong format (not a valid integer)
System.out.println('\nInvalid date and time input.\nPrinting current date and time.\n')//print the error
}
// Print the current date and time values in all supported formats
ZonedDateTime dateAndTimeNow =ZonedDateTime.now() // Get the current date and time based on local computer date and time with local time zone
System.out.println('Current Era: '+ era1.format(dateAndTimeNow) +' ('+ era2.format(dateAndTimeNow) +')'+' ('+ era3.format(dateAndTimeNow) +')') // Print the formatted date and time values in current local date and time
System.out.println('Current ISO Week-based Year: '+ weekBasedYear.format(dateAndTimeNow) +' ('+ weekBasedYear2.format(dateAndTimeNow) +')')
System.out.println('Current Week of Year: '+ weekOfYear.format(dateAndTimeNow))
System.out.println('Current Week of Month: '+ weekOfMonth.format(dateAndTimeNow))
System.out.println('Current Day of the Week: '+ dayOfWeek.format(dateAndTimeNow) +' ('+ dayOfWeek2.format(dateAndTimeNow) +')'+' ('+ dayOfWeek3.format(dateAndTimeNow) +')')
System.out.println('Current Local Day of the Week: '+ localDayOfWeek.format(dateAndTimeNow) +' ('+ localDayOfWeek2.format(dateAndTimeNow) +')'+' ('+ localDayOfWeek3.format(dateAndTimeNow) +')'+' ('+ localDayOfWeek4.format(dateAndTimeNow) +')')
System.out.println('Current Local Week of Month: '+ localWeekOfMonth.format(dateAndTimeNow))
System.out.println('Current Nanosecond of Day: '+ nanosecondOfDay.format(dateAndTimeNow))
System.out.println('Current Time Zone ID: '+ timezoneId.format(dateAndTimeNow))
System.out.println('Current Time Zone: '+ timezoneName2.format(dateAndTimeNow) +' ('+ timezoneName.format(dateAndTimeNow) +')')
System.out.println('Current Localized Time Zone Offset: '+ timezoneOffset.format(dateAndTimeNow) +' ('+ timezoneOffset2.format(dateAndTimeNow) +')')
System.out.println('Current Time Zone Offset: '+ timezoneOffsetZ.format(dateAndTimeNow) +' ('+ timezoneOffsetX.format(dateAndTimeNow) +')'+' ('+ timezoneOffsetZ1.format(dateAndTimeNow) +')')
Instant currentUtcDateAndTime =Instant.now()// Get the current UTC date and time using an Instant object
LocalDateTime utcDateAndTimeNow =LocalDateTime.ofInstant(currentUtcDateAndTime, ZoneOffset.UTC)// Convert from an Instant object to LocalDateTime object
System.out.println('Current UTC Era: '+ era1.format(utcDateAndTimeNow) +' ('+ era2.format(utcDateAndTimeNow) +')'+' ('+ era3.format(utcDateAndTimeNow) +')') // Print the formatted date and time values in current local date and time
System.out.println('Current UTC Year: '+ year1.format(utcDateAndTimeNow) +' ('+ year2.format(utcDateAndTimeNow) +')')
System.out.println('Current UTC Era Year: '+ yearEra.format(utcDateAndTimeNow) +' ('+ yearEra2.format(utcDateAndTimeNow) +')')
System.out.println('Current UTC Day of Year: '+ dayOfYear.format(utcDateAndTimeNow))
System.out.println('Current UTC Month of Year: '+ monthOfYear.format(utcDateAndTimeNow) +' ('+ monthOfYear2.format(utcDateAndTimeNow) +')'+' ('+ monthOfYear3.format(utcDateAndTimeNow) +')'+' ('+ monthOfYear4.format(utcDateAndTimeNow) +')')
System.out.println('Current UTC Day of Month: '+ dayOfMonth.format(utcDateAndTimeNow))
System.out.println('Current UTC ISO Week-based Year: '+ weekBasedYear.format(utcDateAndTimeNow) +' ('+ weekBasedYear2.format(utcDateAndTimeNow) +')')
System.out.println('Current UTC Week of Year: '+ weekOfYear.format(utcDateAndTimeNow))
System.out.println('Current UTC Week of Month: '+ weekOfMonth.format(utcDateAndTimeNow))
System.out.println('Current UTC Day of Week: '+ dayOfWeek.format(utcDateAndTimeNow) +' ('+ dayOfWeek2.format(utcDateAndTimeNow) +')'+' ('+ dayOfWeek3.format(utcDateAndTimeNow) +')')
System.out.println('Current UTC Local Day of the Week: '+ localDayOfWeek.format(utcDateAndTimeNow) +' ('+ localDayOfWeek2.format(utcDateAndTimeNow) +')'+' ('+ localDayOfWeek3.format(utcDateAndTimeNow) +')'+' ('+ localDayOfWeek4.format(utcDateAndTimeNow) +')')
System.out.println('Current UTC Local Week of Month: '+ localWeekOfMonth.format(utcDateAndTimeNow))
System.out.println('Current UTC AM/PM: '+ amPm.format(utcDateAndTimeNow))
System.out.println('Current UTC 12-Hour Format '+ hour12.format(utcDateAndTimeNow))
System.out.println('Current UTC 0-11 Hour Format: '+ hour0To11.format(utcDateAndTimeNow))
System.out.println('Current UTC 24-Hour Format: '+ hour24.format(utcDateAndTimeNow))
System.out.println('Current UTC Hour of Day: '+ hourOfDay.format(utcDateAndTimeNow))
System.out.println('Current UTC Minute: '+ minute1.format(utcDateAndTimeNow))
System.out.println('Current UTC Second: '+ second1.format(utcDateAndTimeNow))
System.out.println('Current UTC Fraction of Second: '+ fractionOfSecond.format(utcDateAndTimeNow))
System.out.println('Current UTC Millisecond of Day: '+ millisecondOfDay.format(utcDateAndTimeNow))
System.out.println('Current UTC Nanosecond: '+ nanosecondOfSecond.format(utcDateAndTimeNow))
System.out.println('Current UTC Nanosecond of Day: '+ nanosecondOfDay.format(utcDateAndTimeNow))