Timezone conversion in Java | Code Factory

1. Convert ZonedDateTime between timezones

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));
}
}
2019-11-30 09:14:27 PM IST
2019-12-01 02:44:27 AM AEDT

2. Convert Date between timezones

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));
}
}
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()));
}
}
2019-11-30 09:15:36 PM IST
2019-12-01 02:45:36 AM AEDT

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store