Java.util.TimeZone Class (Set-2) | Example On TimeZone Class

TimeZone class (the methods of this class was discussed in this article Java.util.TimeZone Class | Set 1) can be used in many cases like using TimeZone class we can get the time difference in term of hour and minute between two places.

Problem : How we can get time difference of time in terms of hours and minutes between two places of Earth?



Solution : To solve above situation, we must follow the following steps

  1. Get the Time Zone Id as input for both the places.
  2. Using this Time Zone Id find the Time Zone of both places.
  3. Using Time Zone Find the Date and Time of both places
  4. Find the difference between the Date and time of both places
  5. The difference is your answer for above question

You can get list of Time Zone Ids then you can go to this link (The data of column name TZ* of this link are Time Zone Ids. You can use those Id as Input.)

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find time difference in term of hour and
// minute between two places.
  
import java.time.LocalDateTime;
import java.util.*;
  
public class TimeDifference {
  
    public static void main(String[] args)
    {
  
        // Take Ids of both places as Input
        Scanner sc = new Scanner(System.in);
  
        String TimeZoneId1 = sc.nextLine();
        String TimeZoneId2 = sc.nextLine();
  
        // Using Id of First place find LocalDateTime of that place
        TimeZone timezone1 = TimeZone.getTimeZone(TimeZoneId1);
  
        LocalDateTime dateTime1 = getDateTime(timezone1);
  
        // Using Id of Second place find LocalDateTime of that place
        TimeZone timezone2 = TimeZone.getTimeZone(TimeZoneId2);
  
        LocalDateTime dateTime2 = getDateTime(timezone2);
  
        // Print the Date and Time of Both TimeZones
        System.out.println("Date and Time of place having Id " + TimeZoneId1);
        System.out.println("Date - " + dateTime1.toLocalDate());
        System.out.println("Time - " + dateTime1.toLocalTime());
        System.out.println("Date and Time of place having Id " + TimeZoneId2);
        System.out.println("Date - " + dateTime2.toLocalDate());
        System.out.println("Time - " + dateTime2.toLocalTime());
  
        // Find the Difference in terms of minutes between both places
        long diffInMinutes = 
            java.time.Duration.between(dateTime1, dateTime2).toMinutes();
        System.out.println("\nDifference in Hour is " 
            + Math.abs(diffInMinutes / 60));
        System.out.println("Difference in Minute is " 
            + Math.abs(diffInMinutes % 60));
    }
  
    static LocalDateTime getDateTime(TimeZone time)
    {
  
        // Using Time zone get calender object
        Calendar cal = new GregorianCalendar(time);
  
        // using calender object find the month, year, day, hour, minute
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
  
        /*
         * construct LocalDateTime object 
        using month, year, day, hour, minute
        */
        LocalDateTime dateTime = LocalDateTime.of(year, month + 1, day,
                                                  hour, minute);
  
        return dateTime;
    }
}

chevron_right


Input:

Asia/Chita
Asia/Yangon

Output:

Date and Time of place having Id Asia/Chita
Date - 2018-04-25
Time - 04:16
Date and Time of place having Id Asia/Yangon
Date - 2018-04-25
Time - 01:46

Difference in Hour is 2
Difference in Minute is 30

Explanation : We are getting TimeZone object of the place using Id provided as Input then using method getDateTime we are getting current Date and Time of that place. The method getDateTime return LocalDateTime object we can get Date using toLocalDate() method of LocalDateTime class and Time using toLocalTime() method of LocalDateTime class.
When we have current date and time of both place then we can find time difference in terms of hour and minute between both places.



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.