java joda_java-Jodatime的开始时间和结束时间
對(duì)于那些來(lái)這里尋找“ js-joda”答案的人,您有兩種選擇,具體取決于要完成的工作
選項(xiàng)1:您希望同一時(shí)區(qū)的一天開(kāi)始
由于您已選擇根據(jù)與時(shí)區(qū)相關(guān)的即時(shí)時(shí)間來(lái)計(jì)算時(shí)間,因此應(yīng)使用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"
選項(xiàng)2:您知道要獲取確切時(shí)間的確切日期
然后,您可以使用LocalDate之外的以下方法來(lái)得出您想要的相對(duì)時(shí)間(即ZonedDateTime):
atStartOfDay(): LocalDateTime
atStartOfDay(zone: ZoneId): ZonedDateTime
atStartOfDayWithZone(zone: ZoneId): ZonedDateTime
選項(xiàng)3:我只希望即時(shí)發(fā)生的那一天
注意使用此代碼,您將獲得相對(duì)于您所處位置的一天。 因此對(duì)于紐約市的人來(lái)說(shuō),它是“ 2019-04-14”,對(duì)于倫敦的人來(lái)說(shuō),它是“ 2019-04-15”(太好了!),因?yàn)闀r(shí)刻是在該時(shí)間段內(nèi) 實(shí)際上是明天在倫敦(“ 2019-04-15T00:00:05Z”)。 假裝您是從紐約市打電話給倫敦的某人,倫敦人會(huì)說(shuō):“老兄,您為什么這么早給我打電話……已經(jīng)是午夜5秒鐘了?!?/p>
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]
總結(jié)
以上是生活随笔為你收集整理的java joda_java-Jodatime的开始时间和结束时间的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java wmi远程桌面服务器_WMI实
- 下一篇: perl java_与Perl相比Jav