In this blog post we will talk about How to convert time from One Time Zone to another in Java? This post also covers some code example so that things are clear to you.
Before proceeding for practical examples few things should be noted :
- You should must know that util.Date class is not TimeZone aware, Object of this class does not store any time zone specific information.
- It is clearly mentioned in the Java documentation for util.Date Class –
- The internal representation of the time inside Date object remains same for a given time, when we print the date object using System.out.println(date) method then date.toString() method is invoked it prints the date object value in local TimeZone of the JVM.
- We can achieve custom TimeZone formatting using SimpleDateFormat The given time in milliseconds can be represented in different TimeZone using different TimeZone formatters using SimpleDateFormat.
- In Java 8 we can achieve Custom TimeZone formatting using ZoneId, ZonedDateTime and DateTimeFormatter
Custom TimeZone formatting using SimpleDateFormat class example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kkjavatutorials.client; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; /** * @author KK JavaTutorials *Custom TimeZone formatting using SimpleDateFormat class */ public class TimeZoneConversionExample1 { public static void main(String[] args) { //Geeting Calendar Instance Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); //Getting DateFormat Instance DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss Z"); //Setting Time for Europe/London dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London")); System.out.println("Time in Europe::"); System.out.println(dateFormat.format(date)); //Setting Time for Asia/Calcutta dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); System.out.println("Time in Asia::"); System.out.println(dateFormat.format(date)); } } |
Sample output of program::
1 2 3 4 |
Time in Europe:: 08/12/2019 08:35:30 +0100 Time in Asia:: 08/12/2019 01:05:30 +0530 |
Custom TimeZone formatting using java 8 APIs class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.kkjavatutorials.client; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; /** * @author KK JavaTutorials *Custom TimeZone formatting using java 8 APIs class */ public class TimeZoneConversionExample2 { private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-M-yyyy hh:mm:ss a z"); public static void main(String[] args) { //Getting Source timezone Id ZoneId sourceTimeZone = ZoneId.of("Asia/Kolkata"); //Getting Target timezone Id ZoneId destinationTimeZone = ZoneId.of("Europe/London"); //Current DateTime time LocalDateTime todayDateTime = LocalDateTime.now(); //Zoned date time at source timezone ZonedDateTime currentISTZonedDateTime = todayDateTime.atZone(sourceTimeZone); //Zoned date time at target timezone ZonedDateTime currentBSTZonedDateTime = currentISTZonedDateTime.withZoneSameInstant(destinationTimeZone); //Now let's format date time System.out.println("Time in Asia::"); System.out.println(dateTimeFormatter.format(currentISTZonedDateTime)); System.out.println("Time in Europe::"); System.out.println(dateTimeFormatter.format(currentBSTZonedDateTime)); } } |
Sample output of program::
1 2 3 4 |
Time in Asia:: 12-8-2019 01:07:30 PM IST Time in Europe:: 12-8-2019 08:37:30 AM BST |
You may also like:
What are the standard ways for handling TimeZone in database transactions?
That’s all about How to convert time from One Time Zone to another in Java?
If you have any feedback or suggestion please feel free to drop in blow comment box.