如何获取一天的开始时间和结束时间? [英] How to obtain the start time and end time of a day?

查看:3334
本文介绍了如何获取一天的开始时间和结束时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取一天的开始时间和结束时间?



这样的代码不准确:



日期日期{
日历日历= Calendar.getInstance();

  private Date getStartOfDay 
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DATE);
calendar.set(年,月,日,0,0,0);
return calendar.getTime();
}

私人日期getEndOfDay(日期日期){
日历calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DATE);
calendar.set(年,月,日,23,59,59);
return calendar.getTime();
}

毫秒之内是不准确的。

解决方案

半开



2.3为此目的提供了一种方法,以获得第一时间天: withTimeAtStartOfDay () 。类似地,在java.time中, LocalDate :: atStartOfDay



搜索StackOverflow forjoda half-open查看更多讨论和示例。



帖子,时间间隔和其他范围应为半数,打开,由Bill Schneider。



避免传统的日期时间类



java.util日期和.Classendar类是非常麻烦的。避免他们



使用 Joda-Time 或者,最好是 java.time 。 java.time框架是非常成功的Joda-Time库的官方继承者。



java.time



java.time框架内置于Java 8及更高版本中。后端移植到Java 6& 7在 ThreeTen-Backport 项目中,进一步适应Android ThreeTenABP 项目。



An Instant 是时间轴上的一段时间 UTC ,分辨率为纳秒。

 即时即时= Instant.now(); 

应用时区获取一些地方的挂钟时间

  ZoneId zoneId = ZoneId.of(America / Montreal); 
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant,zoneId);

要获取当天的第一个时刻,请通过 LocalDate 类及其 atStartOfDay 方法。

  ZonedDateTime zdtStart = zdt.toLocalDate()。atStartOfDay(zoneId ); 

使用Half-Open方法,获得第二天的第一个时刻。

  ZonedDateTime zdtTomorrowStart = zdtStart.plusDays(1); 

目前,java.time框架缺少一个 Interval 类如Joda-Time所述。但是, ThreeTen-Extra 项目将java.time扩展到其他类。这个项目是今后可能添加java.time的证明。其中包括 Interval 。通过传递一对 Instant 对象来构造间隔。我们可以从我们的 ZonedDateTime 对象中提取 Instant

  Interval today = Interval.of(zdtStart.toInstant(),zdtTomorrowStart.toInstant()); 



Joda-Time



Joda-Time有三个课程以各种方式表示时间跨度:间隔期间持续时间。一个间隔在宇宙的时间轴上有一个特定的开始和结尾。这符合我们代表一天的需要。



我们调用方法 withTimeAtStartOfDay 而不是设置一天的时间到零。由于夏令时和其他异常情况,当天的第一时间可能不是 00:00:00



使用Joda-Time 2.3的示例代码。

  DateTimeZone timeZone = DateTimeZone.forID(America / Montreal); 
DateTime now = DateTime.now(timeZone);
DateTime todayStart = now.withTimeAtStartOfDay();
DateTime tomorrowStart = now.plusDays(1).withTimeAtStartOfDay();
Interval today = new Interval(todayStart,tomorrowStart);

如果必须,可以转换为java.util.Date。

  java.util.Date date = todayStart.toDate(); 


How to obtain the start time and end time of a day?

code like this is not accurate:

 private Date getStartOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DATE);
    calendar.set(year, month, day, 0, 0, 0);
    return calendar.getTime();
}

private Date getEndOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DATE);
    calendar.set(year, month, day, 23, 59, 59);
    return calendar.getTime();
}

It is not accurate to the millisecond.

解决方案

Half-Open

The answer by mprivat is correct. His point is to not try to obtain end of a day, but rather compare to "before start of next day". His idea is known as the "Half-Open" approach where a span of time has a beginning that is inclusive while the ending is exclusive.

  • The current date-time frameworks is Java (java.util.Date/Calendar and Joda-Time) both use milliseconds from the epoch. But in Java 8, the new JSR 310 java.time.* classes use nanoseconds resolution. Any code you wrote based on forcing the milliseconds count of last moment of day would be incorrect if switched to the new classes.
  • Comparing data from other sources becomes faulty if they employ other resolutions. For example, Unix libraries typically employ whole seconds, and databases such as Postgres resolve date-time to microseconds.
  • Some Daylight Saving Time changes happen over midnight which might further confuse things.

Joda-Time 2.3 offers a method for this very purpose, to obtain first moment of the day: withTimeAtStartOfDay(). Similarly in java.time, LocalDate::atStartOfDay.

Search StackOverflow for "joda half-open" to see more discussion and examples.

See this post, Time intervals and other ranges should be half-open, by Bill Schneider.

Avoid legacy date-time classes

The java.util.Date and .Calendar classes are notoriously troublesome. Avoid them.

Use either Joda-Time or, preferably, java.time. The java.time framework is the official successor for the highly successful Joda-Time library.

java.time

The java.time framework is built into Java 8 and later. Back-ported to Java 6 & 7 in the ThreeTen-Backport project, further adapted to Android in the ThreeTenABP project.

An Instant is a moment on the timeline in UTC with a resolution of nanoseconds.

Instant instant = Instant.now();

Apply a time zone to get the wall-clock time for some locality.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

To get the first moment of the day go through the LocalDate class and its atStartOfDay method.

ZonedDateTime zdtStart = zdt.toLocalDate().atStartOfDay( zoneId );

Using Half-Open approach, get first moment of following day.

ZonedDateTime zdtTomorrowStart = zdtStart.plusDays( 1 );

Currently the java.time framework lacks an Interval class as described below for Joda-Time. However, the ThreeTen-Extra project extends java.time with additional classes. This project is the proving ground for possible future additions to java.time. Among its classes is Interval. Construct an Interval by passing a pair of Instant objects. We can extract an Instant from our ZonedDateTime objects.

Interval today = Interval.of( zdtStart.toInstant() , zdtTomorrowStart.toInstant() );

Joda-Time

Joda-Time has three classes to represent a span of time in various ways: Interval, Period, and Duration. An Interval has a specific beginning and ending on the timeline of the Universe. This fits our need to represent "a day".

We call the method withTimeAtStartOfDay rather than set time of day to zeros. Because of Daylight Saving Time and other anomalies the first moment of the day may not be 00:00:00.

Example code using Joda-Time 2.3.

DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" );
DateTime now = DateTime.now( timeZone );
DateTime todayStart = now.withTimeAtStartOfDay();
DateTime tomorrowStart = now.plusDays( 1 ).withTimeAtStartOfDay();
Interval today = new Interval( todayStart, tomorrowStart );

If you must, you can convert to a java.util.Date.

java.util.Date date = todayStart.toDate();

这篇关于如何获取一天的开始时间和结束时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆