Java 8 ZonedDateTime class

The java.time.LocalDate class is an immutable class without a time-zone in the ISO-8601 calendar system, such as 2018-04-03.

Java ZonedDateTime class methods

MethodDescription
String format(DateTimeFormatter formatter)It is used to format this date-time using the specified formatter.
int get(TemporalField field)It is used to get the value of the specified field from this date-time as an int.
ZoneId getZone()It is used to get the time-zone, such as ‘Asia/Kolkata’.
ZonedDateTime withZoneSameInstant(ZoneId zone)It is used to return a copy of this date-time with a different time-zone, retaining the instant.
static ZonedDateTime now()It is used to obtain the current date-time from the system clock in the default time-zone.
static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone)It is used to obtain an instance of ZonedDateTime from a local date and time.
ZonedDateTime minus(long amountToSubtract, TemporalUnit unit)It is used to return a copy of this date-time with the specified amount subtracted.
ZonedDateTime plus(long amountToAdd, TemporalUnit unit)It is used to return a copy of this date-time with the specified amount added.

Example

package com.w3spoint;
 
import java.time.Period;
import java.time.ZonedDateTime;
 
public class TestExample {
	public static void main(String args[]){
		ZonedDateTime zone = 
				ZonedDateTime.parse("2018-04-05T08:20:10+04:20[Asia/Kolkata]");  
	    System.out.println(zone);   
 
	    //Get zone
	    System.out.println(zone.getZone());  
 
	    //Use of minus()
	    System.out.println(zone.minus(Period.ofDays(26)));  
 
	    //Use of plus()
	    System.out.println(zone.plus(Period.ofDays(56)));  
	}  
}

Output

2018-04-05T08:20:10+05:30[Asia/Kolkata]
Asia/Kolkata
2018-03-10T08:20:10+05:30[Asia/Kolkata]
2018-05-31T08:20:10+05:30[Asia/Kolkata]
Content Protection by DMCA.com
Please Share