Timezone conversion in Java | Code Factory
2 min readApr 27, 2020
Reference Link : Link
Donate : Link
1. Convert ZonedDateTime between timezones
LocalDateTimeExample.java
package com.codeFactory;import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;/**
* @author code.factory
*
*/
public class LocalDateTimeExample {private static final String DATE_FORMAT = "yyyy-M-dd hh:mm:ss a z";
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT);
private static final String souce = "Asia/Kolkata";
private static final String target = "Australia/Sydney";public static void main(String[] args) {
ZoneId sourceTimeZone = ZoneId.of(souce);
ZoneId targetTimeZone = ZoneId.of(target);LocalDateTime today = LocalDateTime.now();// Zoned date time at source timezone
ZonedDateTime sourceTime = today.atZone(sourceTimeZone);// Zoned date time at target timezone
ZonedDateTime targetTime = sourceTime.withZoneSameInstant(targetTimeZone);System.out.println(FORMATTER.format(sourceTime));
System.out.println(FORMATTER.format(targetTime));
}
}
Output :
2019-11-30 09:14:27 PM IST
2019-12-01 02:44:27 AM AEDT
2. Convert Date between timezones
DateExample.java
package com.codeFactory;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;/**
* @author code.factory
*
*/
public class DateExample {private static final String DATE_FORMAT = "yyyy-M-dd hh:mm:ss a z";
private static final SimpleDateFormat FORMATTER = new SimpleDateFormat(DATE_FORMAT);
private static final String souce = "Asia/Kolkata";
private static final String target = "Australia/Sydney";public static void main(String[] args) {
TimeZone sourceTimeZone = TimeZone.getTimeZone(souce);
TimeZone targetTimeZone = TimeZone.getTimeZone(target);Date currentDate = new Date();// Date in source timezone
FORMATTER.setTimeZone(sourceTimeZone);
System.out.println(FORMATTER.format(currentDate));// Date in target timezone
FORMATTER.setTimeZone(targetTimeZone);
System.out.println(FORMATTER.format(currentDate));
}
}
Output :
2019-11-30 09:15:01 PM IST
2019-12-01 02:45:01 AM AEDT
3. Convert Calendar between timezones
package com.codeFactory;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;/**
* @author code.factory
*
*/
public class CalendarExample {private static final String DATE_FORMAT = "yyyy-M-dd hh:mm:ss a z";
private static final SimpleDateFormat FORMATTER = new SimpleDateFormat(DATE_FORMAT);
private static final String souce = "Asia/Kolkata";
private static final String target = "Australia/Sydney";public static void main(String[] args) {
TimeZone sourceTimeZone = TimeZone.getTimeZone(souce);
TimeZone targetTimeZone = TimeZone.getTimeZone(target);Calendar today = Calendar.getInstance(sourceTimeZone);// Date in source timezone
System.out.println(FORMATTER.format(today.getTime()));// Date in target timezone
FORMATTER.setTimeZone(targetTimeZone);
System.out.println(FORMATTER.format(today.getTime()));
}
}
Output :
2019-11-30 09:15:36 PM IST
2019-12-01 02:45:36 AM AEDT
Reference Links :
All ZoneId
: Get All ZoneId.