CodesJava

Easy learning with example program codes

Java 8 ZoneOffset class


The java.time.ZoneOffset class is used to represent the fixed zone offset from UTC time zone.
ZoneOffset class constants:

  • MAX: maximum supported zone offsets.
  • MIN: minimum supported zone offsets.
  • UTC: time zone offset constant for UTC.

Java ZoneOffset class methods

Method Description
Temporal adjustInto(Temporal temporal) It is used to adjust the specified temporal object to have the same offset as this object.
int get(TemporalField field) It is used to get the value of the specified field from this offset as an int.
boolean isSupported(TemporalField field) It is used to check if the specified field is supported.
static ZoneOffset of(String offsetId) It is used to obtain an instance of ZoneOffset using the ID.
static ZoneOffset ofHours(int hours) It is used to obtain an instance of ZoneOffset using an offset in hours.
static ZoneOffset ofHoursMinutes(int hours, int minutes) It is used to obtain an instance of ZoneOffset using an offset in hours and minutes.
ValueRange range(TemporalField field) It is used to get the range of valid values

Example

package com.codesjava;
 
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;
 
public class TestExample {
	public static void main(String args[]){
	    ZoneOffset zone1 = ZoneOffset.UTC;  
	    Temporal temp = zone1.adjustInto(ZonedDateTime.now());  
	    System.out.println(temp);  
 
	    //ofHours() method
	    ZoneOffset zone2 = ZoneOffset.ofHours(5);  
	    System.out.println(zone2); 
 
	    //ofHoursMinutes() method
	    ZoneOffset zone3 = ZoneOffset.ofHoursMinutes(5,30); 
	    System.out.println(zone3); 
 
 
	}  
}

Output

2018-04-15T19:19:19.620+05:30[Asia/Calcutta]
+05:00
+05:30
Posted in Date   
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki