📜  Java.util.TimeZone 类 (Set-2) | TimeZone 类示例

📅  最后修改于: 2022-05-13 01:55:13.433000             🧑  作者: Mango

Java.util.TimeZone 类 (Set-2) | TimeZone 类示例

TimeZone 类(该类的方法在本文Java.util.TimeZone Class | Set 1 中讨论过)可以在很多情况下使用,例如使用TimeZone 类我们可以获得两地之间以小时和分钟为单位的时差。
问题:我们如何获得地球两地之间的小时和分钟时差?
解决方案:要解决上述情况,我们必须遵循以下步骤

  1. 获取时区 ID 作为两个地方的输入。
  2. 使用此时区 ID 查找两个地方的时区。
  3. 使用时区查找两个地方的日期和时间
  4. 找出两个地方的日期和时间之间的差异
  5. 不同的是您对上述问题的回答

您可以获得时区 ID 列表,然后您可以转到此链接(此链接的列名称 TZ* 的数据是时区 ID。您可以使用这些 ID 作为输入。)

Java
// 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 calendar object
        Calendar cal = new GregorianCalendar(time);
 
        // using calendar 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;
    }
}


输入:

Asia/Chita
Asia/Yangon

输出:

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

说明:我们使用作为输入提供的 Id 获取该地点的 TimeZone 对象,然后使用方法 getDateTime 我们获取该地点的当前日期和时间。 getDateTime 方法返回 LocalDateTime 对象,我们可以使用 LocalDateTime 类的 toLocalDate() 方法获取日期,使用 LocalDateTime 类的 toLocalTime() 方法获取时间。
当我们有两个地方的当前日期和时间时,我们可以找到两个地方之间的小时和分钟的时差。