How to calculate time difference between two Timezones in Java


How to calculate time difference between two Timezones in Java?

1 Answer

7 years ago by

Using following program you can find the difference in two given timezones


import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class DifferenceInTimezones {

	public static void main(String[] args) {
		
		String timeZone1 = "Asia/Kolkata";
		String timeZone2 = "Europe/Paris";
		
		LocalDateTime dt = LocalDateTime.now();
		ZonedDateTime fromZonedDateTime = dt.atZone(ZoneId.of(timeZone1));
		ZonedDateTime toZonedDateTime = dt.atZone(ZoneId.of(timeZone2));
		long diff = Duration.between(fromZonedDateTime, toZonedDateTime).toMillis();
		
		System.out.println("difference between timezones is " + diff + " milliseconds");

	}}

Output:

difference between timezones is 16200000 milliseconds
7 years ago by Karthik Divi