欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

java joda_java-Jodatime的开始时间和结束时间

发布时间:2023/12/2 编程问答 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java joda_java-Jodatime的开始时间和结束时间 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

对于那些来这里寻找“ js-joda”答案的人,您有两种选择,具体取决于要完成的工作

选项1:您希望同一时区的一天开始

由于您已选择根据与时区相关的即时时间来计算时间,因此应使用ZonedDateTime:

import { ZonedDateTime, LocalDate, ZoneId, DateTimeFormatter} from "js-joda";

import 'js-joda-timezone';

const nowInNewYorkCity = ZonedDateTime.now(ZoneId.of("America/New_York"))

const startOfTodayInNYC = nowInNewYorkCity.truncatedTo(ChronoUnit.DAYS);

console.log(startOfTodayInNYC.toString()) // Prints "2019-04-15T00:00-04:00[America/New_York]"

// And if you want to print it in ISO format

console.log(startOfTodayInNYC.format(DateTimeFormatter.ISO_INSTANT)) // "2019-04-14T04:00:00Z"

选项2:您知道要获取确切时间的确切日期

然后,您可以使用LocalDate之外的以下方法来得出您想要的相对时间(即ZonedDateTime):

atStartOfDay(): LocalDateTime

atStartOfDay(zone: ZoneId): ZonedDateTime

atStartOfDayWithZone(zone: ZoneId): ZonedDateTime

选项3:我只希望即时发生的那一天

注意使用此代码,您将获得相对于您所处位置的一天。 因此对于纽约市的人来说,它是“ 2019-04-14”,对于伦敦的人来说,它是“ 2019-04-15”(太好了!),因为时刻是在该时间段内 实际上是明天在伦敦(“ 2019-04-15T00:00:05Z”)。 假装您是从纽约市打电话给伦敦的某人,伦敦人会说:“老兄,您为什么这么早给我打电话……已经是午夜5秒钟了。”

import { ZonedDateTime, LocalDate, ZoneId} from "js-joda";

import 'js-joda-timezone';

const aTimeWhenLondonIsAlreadyInTomorrow = "2019-04-15T00:00:05.000Z";

const inBetweenTimeInLondon = ZonedDateTime.parse(aTimeWhenLondonIsAlreadyInTomorrow);

const inBetweenTimeInNYC = inBetweenTimeInLondon.withZoneSameInstant(ZoneId.of("America/New_York"))

const dayInLondon = inBetweenTimeInLondon.toLocalDate();

const dayInNYC = inBetweenTimeInNYC.toLocalDate();

console.log(inBetweenTimeInLondon.toString()); // "2019-04-15T00:00:05Z"

console.log(dayInLondon.toString()); // "2019-04-15"

console.log(inBetweenTimeInNYC.toString()) // "2019-04-14T20:00:05-04:00[America/New_York]"

console.log(dayInNYC.toString()); // "2019-04-14"

参考:[https://js-joda.github.io/js-joda/class/src/LocalDate.js~LocalDate.html#instance-method-atStartOfDayWithZone]

总结

以上是生活随笔为你收集整理的java joda_java-Jodatime的开始时间和结束时间的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。