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

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();
    

    Java 8


    public static Date atStartOfDay(Date date) {
        LocalDateTime localDateTime = dateToLocalDateTime(date);
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return localDateTimeToDate(startOfDay);
    }
    
    public static Date atEndOfDay(Date date) {
        LocalDateTime localDateTime = dateToLocalDateTime(date);
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return localDateTimeToDate(endOfDay);
    }
    
    private static LocalDateTime dateToLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }
    
    private static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
    

    Update : I've added these 2 methods to my Java Utility Classes here

  • DateUtils.atStartOfDay
  • DateUtils.atEndOfDay
  • It is in the Maven Central Repository at:

    <dependency>
      <groupId>com.github.rkumsher</groupId>
      <artifactId>utils</artifactId>
      <version>1.3</version>
    </dependency>
    

    Java 7 and Earlier


    With Apache Commons

    public static Date atEndOfDay(Date date) {
        return DateUtils.addMilliseconds(DateUtils.ceiling(date, Calendar.DATE), -1);
    }
    
    public static Date atStartOfDay(Date date) {
        return DateUtils.truncate(date, Calendar.DATE);
    }
    

    Without Apache Commons

    public Date atEndOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
    
    public Date atStartOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
    

    in getEndOfDay, you can add:

    calendar.set(Calendar.MILLISECOND, 999);
    

    Although mathematically speaking, you can't specify the end of a day other than by saying it's "before the beginning of the next day".

    So instead of saying, if(date >= getStartOfDay(today) && date <= getEndOfDay(today)) , you should say: if(date >= getStartOfDay(today) && date < getStartOfDay(tomorrow)) . That is a much more solid definition (and you don't have to worry about millisecond precision).

    链接地址: http://www.djcxy.com/p/18512.html

    上一篇: DateTime“null”值

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