我该如何计算,明天剩下多少时间?

发布于 2025-02-10 04:52:28 字数 1490 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℡Ms空城旧梦 2025-02-17 04:52:28

SimpleDateFormat和相关的旧类是多年前由JSR 310中定义的现代Java.Time 类取代的。

确定“明天”需要时区。当您穿过时区向东移动时,几天开始。

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;

捕获通过该时区看到的当前时刻。

ZonedDateTime now = ZonedDateTime.now( z ) ;

现在,我们有能力确定“明天”,特别是第二天的第一时刻。

提取日期部分。

LocalDate today = now.toLocalDate() ;
LocalDate tomorrow = today.plus( 1 ) ;

获得第一个时刻。请注意,我们做不是假定一天开始于00:00

SimpleDateFormat and related legacy classes were years ago supplanted by the modern java.time classes defined in JSR 310.

Determining “tomorrow” requires a time zone. Days start earlier as you move eastward through time zones.

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;

Capture the current moment as seen through that time zone.

ZonedDateTime now = ZonedDateTime.now( z ) ;

Now we are in a position to determine “tomorrow”, specifically the first moment of the following day.

Extract the date portion.

LocalDate today = now.toLocalDate() ;
LocalDate tomorrow = today.plus( 1 ) ;

Get first moment. Note that we do not assume the day starts at 00:00 ????. Some dates in some zones start at a different time of day, such as 01:00 ????. Let java.time determine the first moment.

ZonedDateTime startTomorrowTokyo = tomorrow.atStartOfDay( z ) ;

Calculate time to elapse.

Duration untilTomorrow = Duration.between ( now , startTomorrowTokyo ) ;

Generate text in standard ISO 8601 format.

String output = untilTomorrow.toString() ;

If you insist on the risky ambiguous use of clock-time to represent the duration, you can build your own string by interrogating the Duration object. Call its to…Part methods.

淡莣 2025-02-17 04:52:28
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Duration;


LocalDateTime tomorrowMidnight = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT).plusDays(1);        
        
Duration duration = Duration.between(LocalDateTime.now(), tomorrowMidnight);
System.out.printf("%d:%d", duration.toHours(), duration.toMinutes()%60);
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Duration;


LocalDateTime tomorrowMidnight = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT).plusDays(1);        
        
Duration duration = Duration.between(LocalDateTime.now(), tomorrowMidnight);
System.out.printf("%d:%d", duration.toHours(), duration.toMinutes()%60);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文