Java 8 ZonedDateTime example.
ZonedDateTime class is a date-time with a time-zone in the ISO-8601 calendar system, such as 2016-12-03T10:15:30+01:00 Europe/Paris.
ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time
fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.
In terms of design, this class should be viewed primarily as the combination of a LocalDateTime and a ZoneId.
The ZoneOffset is a vital, but secondary, piece of information, used to ensure that the class represents an instant,
especially during a daylight savings overlap.
ZonedDateTime class is immutable and thread-safe.
ZonedDateTimeEx |
package com.java2novice.datetime;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTimeEx {
public static void main(String[] args) {
ZonedDateTime zoneDateTime = ZonedDateTime.now();
System.out.println("current date time with zone: "+zoneDateTime);
// get date alone
LocalDate d1 = zoneDateTime.toLocalDate();
System.out.println("current date: " + d1);
// get zone info
ZoneId zone = zoneDateTime.getZone();
System.out.println("zone info: " + zone.getId());
// extract date properties
Month mnt = zoneDateTime.getMonth();
System.out.println("Month: "+mnt.getValue());
int dayOfMonth = zoneDateTime.getDayOfMonth();
System.out.println("Day of the month: "+dayOfMonth);
int year = zoneDateTime.getYear();
System.out.println("Year: "+year);
// extract time properties
int hour = zoneDateTime.getHour();
System.out.println("current hour: "+hour);
int minutes = zoneDateTime.getMinute();
System.out.println("current minutes: "+minutes);
int seconds = zoneDateTime.getSecond();
System.out.println("current seconds: "+seconds);
}
}
|
|