如何在Android的另一个时区中获取一天的开始时间和结束时间 [英] How to get start time and end time of a day in another timezone in Android

查看:204
本文介绍了如何在Android的另一个时区中获取一天的开始时间和结束时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期格式为:YYYY-MM-DD

I've the date in format: YYYY-MM-DD

所需的输出格式为:
yyyy-MM-dd T'HH:mm:ss.SSS'Z'

我想将ISO格式的日期作为一天的开始时间(开始从 America / Chicago 时区开始到一天的结束时间(结束于11:59 PM)。

And I want to get the date in ISO format as Start time of the day(starts from 12:00AM) and end time of the day(ends on 11:59PM) in America/Chicago timezone.

例如对于日期:2020-06-08(6月8日),转换后的最终输出如下:

For eg. For the date: 2020-06-08 (June 8) the converted final output is like:

一天的开始时间为日期:2020-06- 08T05:00:00.000Z

一天的结束时间为日期:2020-06-09T04:59:59.999Z

如果有人对此有任何线索,请在这里帮助我。

Please help me here if anybody has any clue for the same.

答案: http://m.uploadedit.com/bltc/1591511729781.txt

推荐答案

java.time和ThreeTenABP



我建议使用java.time ,现代Java日期和时间API,用于此类日期和时间数学。

java.time and ThreeTenABP

I suggest using java.time, the modern Java date and time API, for such date and time math.

    ZoneId zone = ZoneId.of("America/Chicago");

    String dateString = "2020-06-08";
    LocalDate date = LocalDate.parse(dateString);
    Instant startOfDay = date.atStartOfDay(zone).toInstant();
    Instant endOfDay = date.plusDays(1).atStartOfDay(zone).toInstant();

    System.out.println("The day is from " + startOfDay + " (inclusive) to " + endOfDay + " (exclusive)");

输出为:


这一天是从2020-06-08T05:00:00Z(含)到
2020-06-09T05:00:00Z(不包含)

The day is from 2020-06-08T05:00:00Z (inclusive) to 2020-06-09T05:00:00Z (exclusive)

为了不排除一天的最后一毫秒,我们需要将直到第二天的第一天算在内。如果您确实坚持要减去毫秒或十亿分之一秒,那当然取决于您。例如:

In order not to exclude the last millisecond of the day we need to count the day as lasting up to the first moment of the next day exclusive. If you do insist on subtracting a millisecond or just a nanosecond, it’s up to you to do so, of course. For example:

    Instant endOfDay = date.plusDays(1).atStartOfDay(zone).minusNanos(1).toInstant();




这一天是从2020-06-08T05:00:00Z(含)到
2020-06-09T04:59:59.999999999Z(含1纳米及更多)

The day is from 2020-06-08T05:00:00Z (inclusive) to 2020-06-09T04:59:59.999999999Z (inclusive and 1 nano more)

编辑:如何在输出中获得毫秒数?您很可能不需要。您要求的格式是国际标准ISO 8601(请参阅底部的链接)。在ISO 8601中,秒的小数部分是可选的,而省略它意味着0。因此,任何需要ISO 8601的API都应接受上述内容。除非有,否则正确的解决方案是使用格式化程序。在这种情况下,我们需要将时间明确转换为UTC:

How to get milliseconds printed in the output? You most probably don’t need that. The format you are asking for is ISO 8601 (see the link at the bottom), the international standard. In ISO 8601 the fraction of second is optional, and leaving it out implies 0. So any API requiring ISO 8601 should accept the above. Only if not, the correct solution is to use a formatter. In this case we need to convert the time to UTC explicitly:

    ZoneId zone = ZoneId.of("America/Chicago");
    DateTimeFormatter formatterWithMillis = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");

    String dateString = "2020-06-08";
    LocalDate date = LocalDate.parse(dateString);
    OffsetDateTime startOfDay = date.atStartOfDay(zone)
            .toOffsetDateTime()
            .withOffsetSameInstant(ZoneOffset.UTC);
    OffsetDateTime endOfDay = date.plusDays(1)
            .atStartOfDay(zone)
            .toOffsetDateTime()
            .withOffsetSameInstant(ZoneOffset.UTC);

    String startString = startOfDay.format(formatterWithMillis);
    String endString = endOfDay.format(formatterWithMillis);
    System.out.println("The day is from " + startString + " (inclusive) to " + endString + " (exclusive)");




这一天是从2020-06-08T05:00:00.000Z (含)到
2020-06-09T05:00:00.000Z(不含)

The day is from 2020-06-08T05:00:00.000Z (inclusive) to 2020-06-09T05:00:00.000Z (exclusive)

结束时间与以前相同:

    OffsetDateTime endOfDay = date.plusDays(1)
            .atStartOfDay(zone)
            .minusNanos(1)
            .toOffsetDateTime()
            .withOffsetSameInstant(ZoneOffset.UTC);




这一天是从2020-06-08T05:00:00.000Z (含)到
2020-06-09T04:59:59.999Z(含1纳米及更多)

The day is from 2020-06-08T05:00:00.000Z (inclusive) to 2020-06-09T04:59:59.999Z (inclusive and 1 nano more)

(自时间缩短到整毫秒, 1纳秒并没有什么意义,但我认为这不是重点。)

(Since the time is truncated to whole milliseconds the 1 nano more doesn’t really make sense, but I don’t think that’s the point here.)

java.time在较新和较旧的Android设备上均可正常运行。它只需要至少 Java 6

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.


  • 在Java 8和更高版本以及较新的Android设备上(通过API)级别26)是内置的现代API。

  • 在非Android Java 6和7中,获取ThreeTen Backport,即现代类的Backport(JSR 310的ThreeTen;请参见链接

  • 在(较旧的)Android上,请使用Android版的ThreeTen Backport。叫做ThreeTenABP。并确保使用子包从 org.threeten.bp 导入日期和时间类。

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

但是除了切换到ThreeTenBP
库之外,我们是否还有其他选择?

But don't we have any other option apart from switching to ThreeTenBP Library?

如果您坚持使用,我想这是使用日历日期和<$ c $的方法c> SimpleDateFormat 可以找到。这些类的设计很差,而且已经过时,因此,根据我所知和不知道,我更喜欢ThreeTenABP。

If you insisted, I suppose that a way through using Calendar, Date and SimpleDateFormat could be found. Those classes are poorly designed and long outdated, so with what I know and don’t know I would prefer ThreeTenABP.

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
  • Wikipedia article: ISO 8601

这篇关于如何在Android的另一个时区中获取一天的开始时间和结束时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆