Let’s examine how to change the time zone of a date in java in different ways.
How to change the timezone of a java.util.Date using SimpleDateFormat
We must be clear that the Date class in Java does not have a time zone.
java.util.Date represents the number of seconds from midnight January 1, 1970 to zero hours (UTC time).
This means, java.util.Date will always return the time for the time zone that is by default defined for the system.
What you need to do to change the time zone to a Date is:
- Create a DateFormat with the time zone for the area you want
- Apply the DateFormat to the Date by obtaining a string with modified
- Recreate an object Date from that string
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class TimeZoneChangeDateExample {
private static final String DATE_FORMAT = "dd-MM-yyyy hh:mm:ss a";
public static void main(String[] args) throws ParseException {
// the date
Calendar cal = Calendar.getInstance();
cal.set(2017, 06, 29, 8, 30);
Date date = cal.getTime();
// format with tz
TimeZone timeZone = TimeZone.getTimeZone("Europe/Amsterdam");
SimpleDateFormat formatterWithTimeZone = new SimpleDateFormat(DATE_FORMAT);
formatterWithTimeZone.setTimeZone(timeZone);
// change tz using formatter
String sDate = formatterWithTimeZone.format(date);
// string to object date
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
Date dateWithTimeZone = formatter.parse(sDate); // string to Date Object
System.out.println("The actual date is: " + formatter.format(date));
System.out.println("The date in " + timeZone.getID() + " is: " + sDate);
System.out.println("Object date: " + formatter.format(dateWithTimeZone));
}
}
The output of this example.
The actual date is: 29-07-2017 08:30:54 AM
The date in Europe/Amsterdam is: 29-07-2017 01:30:54 PM
Object date: 29-07-2017 01:30:54 PM
How to change the timezone of a Date using Calendar
To change the time zone using the calendar class you must:
- Create a Calendar
- Setting time zone to calendar
- Get the values through the specific gets.
Note: if you try to get the date directly, calendar.getTime(), you will get the date always passed to the local time because what it returns is a java.util.Date and as we said before this one does not have the time zone.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
public class TimeZoneChangeCalendarExample {
private static final String DATE_FORMAT = "dd-MM-yyyy hh:mm:ss a";
public static void main(String[] args) throws ParseException {
// calendar with tx
TimeZone timeZone = TimeZone.getTimeZone("Europe/Amsterdam");
Calendar calendar = Calendar.getInstance();
calendar.set(2017, 06, 29, 8, 30);
calendar.setTimeZone(timeZone);
// get calendar using fields
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
System.out.println("The actual date is: " + formatter.format(calendar.getTime()));
System.out.print("The date in " + timeZone.getID() + " is: ");
System.out.print(" Day:" + dayOfMonth);
System.out.print(" Month:" + (month + 1)); // base zero
System.out.print(" Year:" + year);
System.out.print(" Hour:" + hourOfDay);
System.out.print(" Min:" + minute);
System.out.print(" Sec:" + second);
System.out.println();
// calendar getTime with DateFormat
SimpleDateFormat formatterWithTimeZone = new SimpleDateFormat(DATE_FORMAT);
formatterWithTimeZone.setTimeZone(timeZone);
System.out.println("Calendar.getTime() - Date in Local TZ:" + formatterWithTimeZone.format(calendar.getTime()));
}
}
Output.
The actual date is: 29-07-2017 03:30:22 AM
The date in Europe/Amsterdam is: Day:29 Month:7 Year:2017 Hour:8 Min:30 Sec:22
Calendar.getTime() - Date in Local TZ:29-07-2017 08:30:22 AM
How to change the timezone of a Date using Java 8+
To change the time zone of a date in Java 8+:
- Create a LocalDateTime
- Setting the original time zone at LocalDateTime
- Change the time zone of the original date
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class TimeZoneChangeZonedDateTime {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2018, 06, 29, 8, 30);
// zone from Buenos Aires
ZoneId zoneBuenosAires = ZoneId.of("America/Buenos_Aires");
ZonedDateTime asiaZonedDateTime = localDateTime.atZone(zoneBuenosAires);
// zone from Amsterdan
ZoneId zoneAmsterdan = ZoneId.of("Europe/Amsterdam");
ZonedDateTime nyDateTime = asiaZonedDateTime.withZoneSameInstant(zoneAmsterdan);
// print dateTime with tz
System.out.println("Date : " + asiaZonedDateTime);
System.out.println("Date : " + nyDateTime);
}
}
Output:
Date : 2018-06-29T08:30-03:00[America/Buenos_Aires]
Date : 2018-06-29T13:30+02:00[Europe/Amsterdam]
Conclusion
This post shows three ways to change the time zone for a date, starting on how to change the time zone for a java.util.Date, then for a java.util.Calendar and finally in Java8+ making use of the new api java.time.
Code: Code of this post here