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
- Get the Time Zone Id as input for both the places.
- Using this Time Zone Id find the Time Zone of both places.
- Using Time Zone Find the Date and Time of both places
- Find the difference between the Date and time of both places
- 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.)
Java
import java.time.LocalDateTime;
import java.util.*;
public class TimeDifference {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String TimeZoneId1 = sc.nextLine();
String TimeZoneId2 = sc.nextLine();
TimeZone timezone1 = TimeZone.getTimeZone(TimeZoneId1);
LocalDateTime dateTime1 = getDateTime(timezone1);
TimeZone timezone2 = TimeZone.getTimeZone(TimeZoneId2);
LocalDateTime dateTime2 = getDateTime(timezone2);
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());
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)
{
Calendar cal = new GregorianCalendar(time);
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);
LocalDateTime dateTime = LocalDateTime.of(year, month + 1 , day,
hour, minute);
return dateTime;
}
}
|
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.
Sure, here’s an example of using the TimeZone class in Java:
Java
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
TimeZone defaultZone = TimeZone.getDefault();
System.out.println( "Default time zone: " + defaultZone.getID());
TimeZone losAngelesZone = TimeZone.getTimeZone( "America/Los_Angeles" );
TimeZone.setDefault(losAngelesZone);
System.out.println( "Current time zone: " + TimeZone.getDefault().getID());
String[] availableZones = TimeZone.getAvailableIDs();
System.out.println( "Available time zones:" );
for (String zone : availableZones) {
System.out.println(zone);
}
}
}
|
Output:
Default time zone: America/New_York
Current time zone: America/Los_Angeles
Available time zones:
…
America/Cuiaba
America/Curacao
America/Danmarkshavn
America/Dawson
America/Dawson_Creek
America/Denver
America/Detroit
…
In this example, we’re using the TimeZone class to get and set the time zone, as well as to display a list of available time zones.
First, we’re using the getDefault() method to get the default time zone of the system and printing it out. Then, we’re setting the time zone to Los Angeles using the getTimeZone() method and the setDefault() method. We’re then printing out the current time zone to confirm that it has been set to Los Angeles.
Finally, we’re using the getAvailableIDs() method to get a list of all available time zones and printing them out in the console.
Overall, the TimeZone class in Java is useful for working with time zones and performing various operations on them, such as getting the default time zone, setting the time zone, and getting a list of available time zones.
Here are some advantages and disadvantages of using the TimeZone class in Java:
Advantages:
- The TimeZone class provides a standard way to work with time zones, making it easier to write portable code that works across different platforms and systems.
- It offers a wide range of functionality for working with time zones, such as getting the default time zone, setting the time zone, getting a list of available time zones, and converting times between different time zones.
- It can help prevent errors that can occur when working with time zones manually, such as forgetting to adjust for daylight saving time or forgetting to account for differences in time zones.
Disadvantages:
- The TimeZone class can be complex and difficult to use, particularly for developers who are new to working with time zones.
- The large number of available time zones can make it difficult to select the correct one for a given use case.
- The TimeZone class can be slow and resource-intensive, particularly when performing complex operations that involve many time zone conversions.
Overall, the TimeZone class is a useful tool for working with time zones in Java, but it may not be the best choice for all use cases. Developers
- should carefully consider their specific needs and use cases when deciding whether or not to use the TimeZone class, and should be prepared to invest time and effort into learning how to use it effectively.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Mar, 2023
Like Article
Save Article