代码之家  ›  专栏  ›  技术社区  ›  Illustrious Imp

以epochmillis为不同时区获取一天的开始时间和结束时间-java

  •  1
  • Illustrious Imp  · 技术社区  · 2 年前

    我正在尝试获取PST时区中一天的开始时间(00:00:00)和结束时间(23:59:59)。我尝试了以下代码,但由于某些原因,我只得到UTC的开始和结束时间。我曾尝试将时区更改为包含“America/Los_angeles”,但输出时间戳总是显示GMT/UTC的开始和结束时间。

    我的代码:

    val time_zone = ZoneId.of("America/Los_Angeles")
    val today_date = LocalDate.now(time_zone).plusDays(0)
    val start_time = today_date + " " + "00:00:00"
    val end_time = today_date + " " + "23:59:59"
    
    val date_format = new SimpleDateFormat("yyyy-MM-dd");
    val start_millis = date_format.parse(start_time).getTime();
    val end_millis = date_format.parse(end_time).getTime();
    
    start_millis
    

    输出:

    res375: Long = 1656460799000
    

    在历元转换器中, 1656460799000 给我这个: The start time is 00:00 in UTC, not in America/Los_Angeles time

    我这里缺什么?我是否应该更新任何包等。?

    1 回复  |  直到 2 年前
        1
  •  2
  •   Basil Bourque    2 年前

    Java语言时间

    现代方法使用 Java语言时间 仅限课程。

    无需使用 SimpleDateFormat , Date , Calendar ,以及其他可怕的遗留日期时间类。如果需要,可以通过添加到旧类中的新转换方法进行来回转换。

    一天的开始

    我正在尝试获取开始时间(00:00:00)

    不要假设一天从00:00开始。某些区域中的某些日期在其他时间开始,例如01:00。允许 Java语言时间 使用确定一天中的第一个时刻 LocalDate#atStartOfDay .

    一天结束时

    一天的结束时间(23:59:59)

    这种方法会让你错过一整天的最后一秒。

    日期-时间工作通常采用半开放式方法。在半开的情况下,开始是 包含全部费用 而结局是 独家 . 因此,一天从一天的第一刻开始,一直持续到第二天的第一刻,但不包括第二天的第一刻。

    时区

    PST时区。

    没有所谓的时区 PST . 这种2-4个字母的伪时区被大众媒体用来表示关于时区的提示。但这些伪区 标准化,甚至都不是唯一的!仅用于向人类进行本地化演示,绝不用于数据存储或数据交换。

    Real time zones 命名为 Continent/Region .

    也许你所说的太平洋标准时间是指太平洋标准时间,它通常表示 America/Tijuana America/Los_Angeles America/Vancouver 或其他。

    或者你所说的PST是指菲律宾标准时间 Asia/Manila 时区。

    示例代码

    捕捉时区中的当前时刻。

    ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
    ZonedDateTime zdt = ZonedDateTime.now( z ) ;
    

    提取日期。

    LocalDate today = zdt.toLocalDate() ;
    

    确定一天中的第一个时刻。

    ZonedDateTime zdtStartOfDay = today.atStartOfDay( z ) ;
    

    并确定第二天的第一个时刻。

    ZonedDateTime zdtStartOfFollowingDay = today.plusDays( 1 ).atStartOfDay( z ) ;
    

    你可能想看看时间的长短。并非所有的日子都是24小时。

    Duration d = Duration.between( zdtStartOfDay , zdtStartOfFollowingDay ) ;
    

    通过提取 Instant 对象该类表示UTC中的一个时刻。

    Instant start = zdtStartOfDay.toInstant() ;
    Instant end = zdtStartOfFollowingDay.toInstant() ;
    

    对于每个,获取自1970年第一个时刻的历元参考以来的毫秒计数,如UTC 1970-01-01T00:00Z所示。

    long startMilli = start.toEpochMilli() ;
    long endMilli = end.toEpochMilli() ;
    

    但是,我强烈建议不要以毫秒为单位跟踪时间。这种方法令人困惑,因为至少 couple dozen epoch reference 点是常用的。和a long 不能被人类读者解读,所以错误可能会被忽视。

    相反,数据存储和数据交换通常应使用标准作为文本进行 ISO 8601 格式。这个 Java语言时间 类在解析/生成文本时默认使用这些标准格式。

    String startText = start.toString() ;
    String endText = end.toString() ;
    

    额外三十元

    您可能需要添加 ThreeTen-Extra 库到您的项目。这使您可以访问 Interval 类,将时间跨度表示为一对 瞬间 物体。

    Interval allDayLongToday = org.threeten.extra.Interval.of( start , end ) ;
    
        2
  •  0
  •   Halil Sahin    2 年前

    只需将此部分添加到代码中:

    date_format.setTimeZone(TimeZone.getTimeZone("PST"));
    

    然后它将按您的需要工作:)