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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Managed Kubernetes: Is The Cheapest Really Cheap?

Let’s Get Basic:

Explain Difference between formal and actual parameter with example.

Are you releasing as often as you can?

Why 2019 is an important year for Microsoft Azure certifications

Parallels For Mac Load From Iso

Spark SQL — ROW_NUMBER VS RANK VS DENSE_RANK

Agile without Automation is reckless

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
Code Factory

Code Factory

More from Medium

How to create a system for Pet Care Store in Java?

Why Spring Boot is called opinionated?

Exception handling In Java.

Null check strategy in HTTP PATCH with Java 8 Consumer