如何找到日期中小时和分钟的相等性。

huangapple go评论78阅读模式
英文:

How to find equality of hr, min in date

问题

我需要在Java中查找日期中小时和分钟的相等性(一个日期在夏时制后,另一个日期在夏时制结束后),这两个日期都处于UTC时区。英国于2020-10-26 早上2点结束夏时制,所以在上述示例中,小时和分钟是相等的。

  1. 日期1 = 2020-10-22T07:00:00+0000
  2. 日期2 = 2020-10-26T08:00:00+0000
英文:

I need to find equality of hour, minutes in date (1 date in DST, 1 date after DST) in java. Both dates are in UTC. DST ends in UK 2020-10-26 2 AM, so in above example, hour and minute are equal.

  1. Date1 = 2020-10-22T07:00:00+0000
  2. Date2 = 2020-10-26T08:00:00+0000

答案1

得分: 2

ZoneId zoneUk = ZoneId.of("Europe/London");

ZonedDateTime a = ZonedDateTime.parse("2020-10-22T07:00:00+00:00").withZoneSameInstant(zoneUk);
ZonedDateTime b = ZonedDateTime.parse("2020-10-26T08:00:00+00:00").withZoneSameInstant(zoneUk);

Notice the additional `:` in the time offset or it can't be parsed. 

System.out.println(a); // 2020-10-22T08:00+01:00[Europe/London]
System.out.println(b); // 2020-10-26T08:00Z[Europe/London]

System.out.println(a.toLocalTime()); // 08:00
System.out.println(b.toLocalTime()); // 08:00

System.out.println(a.toLocalTime().equals(b.toLocalTime())); // true
英文:
ZoneId zoneUk = ZoneId.of("Europe/London");

ZonedDateTime a = ZonedDateTime.parse("2020-10-22T07:00:00+00:00").withZoneSameInstant(zoneUk);
ZonedDateTime b = ZonedDateTime.parse("2020-10-26T08:00:00+00:00").withZoneSameInstant(zoneUk);

Notice the additional : in the time offset or it can't be parsed.

System.out.println(a); // 2020-10-22T08:00+01:00[Europe/London]
System.out.println(b); // 2020-10-26T08:00Z[Europe/London]
  
System.out.println(a.toLocalTime()); // 08:00
System.out.println(b.toLocalTime()); // 08:00
  
System.out.println(a.toLocalTime().equals(b.toLocalTime())); // true

答案2

得分: 2

import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // 给定的日期时间字符串
        String strDate1 = "2020-10-22T07:00:00+0000";
        String strDate2 = "2020-10-26T08:00:00+0000";

        // 为给定的日期时间字符串定义格式化程序
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("u-M-d'T'H:m:sZ");

        // 英国的时区
        ZoneId tzLondon = ZoneId.of("Europe/London");

        // 获取在英国的对应日期时间
        ZonedDateTime zdt1 = OffsetDateTime.parse(strDate1, formatter).atZoneSameInstant(tzLondon);
        ZonedDateTime zdt2 = OffsetDateTime.parse(strDate2, formatter).atZoneSameInstant(tzLondon);

        System.out.println(zdt1);
        System.out.println(zdt2);

        // 从 ZonedDateTime 获取本地时间
        LocalTime lt1 = zdt1.toLocalTime();
        LocalTime lt2 = zdt2.toLocalTime();

        System.out.println(lt1);
        System.out.println(lt2);
    }
}

输出:

2020-10-22T08:00+01:00[Europe/London]
2020-10-26T08:00Z[Europe/London]
08:00
08:00
英文:

Your date-time strings have Zone-Offset of +0000 and therefore parsing them into OffsetDateTime (using an appropriate DateTimeFormatter) will be a more natural choice. Once you have parsed them into OffsetDateTime, convert them to the ZonedDateTime corresponding to the time-zone of the UK. As a final step, you need to get the local time part of the ZonedDateTime.

import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
	public static void main(String[] args) {
		// The given date-time strings
		String strDate1 = "2020-10-22T07:00:00+0000";
		String strDate2 = "2020-10-26T08:00:00+0000";

		// Define the formatter for the given date-time strings
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("u-M-d'T'H:m:sZ");

		// ZoneId of the UK
		ZoneId tzLondon = ZoneId.of("Europe/London");

		// Get the corresponding date-time in the UK
		ZonedDateTime zdt1 = OffsetDateTime.parse(strDate1, formatter).atZoneSameInstant(tzLondon);
		ZonedDateTime zdt2 = OffsetDateTime.parse(strDate2, formatter).atZoneSameInstant(tzLondon);

		System.out.println(zdt1);
		System.out.println(zdt2);

		// Get local date from ZonedDateTime
		LocalTime lt1 = zdt1.toLocalTime();
		LocalTime lt2 = zdt2.toLocalTime();

		System.out.println(lt1);
		System.out.println(lt2);
	}
}

Output:

2020-10-22T08:00+01:00[Europe/London]
2020-10-26T08:00Z[Europe/London]
08:00
08:00

huangapple
  • 本文由 发表于 2020年10月13日 11:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64328064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定