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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值