Friday 22 October 2021

How to set JVM timezone?

In this post, I am going to explain multiple ways to set the timezone of JVM.

 

Approach 1: Using TimeZone.setDefault method.

Example

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

 

Find the below working application.

 

JvmTimeZone1.java

package com.sample.app.time;

import java.util.Date;
import java.util.TimeZone;

public class JvmTimeZone1 {

	public static void main(String args[]) {

		System.out.println("Default time zone : " + TimeZone.getDefault());
		System.out.println("Date -> " + new Date());
		
		System.out.println("\nSetting the time zone to UTC");
		TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

		System.out.println("Date -> " + new Date());

		System.out.println("\nSetting the time zone to Europe/Sofia");
		TimeZone.setDefault(TimeZone.getTimeZone("Europe/Sofia"));

		System.out.println("Date -> " + new Date());

	}

}

 

Output

Default time zone : sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
Date -> Fri Oct 22 14:32:35 IST 2021

Setting the time zone to UTC
Date -> Fri Oct 22 09:02:35 UTC 2021

Setting the time zone to Europe/Sofia
Date -> Fri Oct 22 12:02:35 EEST 2021

 

Approach 2: By setting the system property 'user.timezone'

Example

System.setProperty("user.timezone", "UTC");

 

JvmTimeZone2.java

package com.sample.app.time;

import java.util.Date;
import java.util.TimeZone;

public class JvmTimeZone2 {
	public static void main(String args[]) {

		System.setProperty("user.timezone", "UTC");
		System.out.println("Default time zone : " + TimeZone.getDefault());
		System.out.println("Date -> " + new Date());

	}
}

 

Output

Default time zone : sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
Date -> Fri Oct 22 09:12:33 UTC 2021

 

Approach 3: By passing the VM argument ‘-Duser.timezone’

 

Example

-Duser.timezone=Europe/Sofia

 

JvmTimeZone3.java

package com.sample.app.time;

import java.util.Date;
import java.util.TimeZone;

public class JvmTimeZone3 {
	public static void main(String args[]) {

		System.out.println("Default time zone : " + TimeZone.getDefault());
		System.out.println("Date -> " + new Date());

	}
}

 

Run above application by passing the jvm argument ‘-Duser.timezone=Europe/Sofia’, you will see below kind of output.

Default time zone : sun.util.calendar.ZoneInfo[id="Europe/Sofia",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=125,lastRule=java.util.SimpleTimeZone[id=Europe/Sofia,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
Date -> Fri Oct 22 12:24:54 EEST 2021

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment