Getting the time range between midnight and current JDK 8 time

I have this method for calculating the average and current time as long values:

/**
 * Returns the time range between the midnight and current time in milliseconds.
 *
 * @param zoneId time zone ID.
 * @return a {@code long} array, where at index: 0 - midnight time; 1 - current time.
 */

public static long[] todayDateRange(ZoneId zoneId) {
    long[] toReturn = new long[2];

    LocalTime midnight = LocalTime.MIDNIGHT;
    LocalDate today = LocalDate.now(zoneId);
    LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
    ZonedDateTime todayMidnightZdt = todayMidnight.atZone(zoneId);
    toReturn[0] = todayMidnightZdt.toInstant().toEpochMilli();

    ZonedDateTime nowZdt = LocalDateTime.now().atZone(zoneId);
    toReturn[1] = nowZdt.toInstant().toEpochMilli();
    return toReturn;
}

Perhaps there is an easier way to do this?

+4
source share
2 answers

You can also do:

ZonedDateTime nowZdt = ZonedDateTime.now(zoneId);

ZonedDateTime todayAtMidnightZdt = nowZdt.with(LocalTime.MIDNIGHT);

I cannot think of an easier way to do this.


LocalDateTime vs ZonedDateTime

There is a (complicated) difference between LocalDateTime.now().atZone(zoneId)and ZonedDateTime.now(zoneId).

JVM, America/Sao_Paulo, (Europe/London). , 20 th 2017, - 17:56, 21:56.

:

LocalDateTime nowLdt = LocalDateTime.now();

LocalDateTime JVM . - ( 20 th 2017, 17:56):

2017-08-20T 17:56: 05,159

atZone, ZonedDateTime, :

ZoneId zoneId = ZoneId.of("Europe/London");
ZonedDateTime nowAtZone = nowLdt.atZone(zoneId);

nowAtZone :

2017-08-20T 17:56: 05,159 + 01: 00 [Europe/London]

(20 th 2017) (17:56) . , / . Milli:

System.out.println(nowAtZone.toInstant().toEpochMilli());

:

1503248165159

, LocalDateTime direclty, ZonedDateTime :

ZonedDateTime nowZdt = ZonedDateTime.now(zoneId);

, :

2017-08-20T 21:56: 05,170 + 01: 00 [Europe/London]

, ( 21:56). , , , . epochMilli:

System.out.println(nowZdt.toInstant().toEpochMilli());

:

1503262565170

, LocalDateTime ( , ). , ZonedDateTime.now(zoneId).

LocalDateTime.now().atZone() , , JVM JVM (- , TimeZone.setDefault()).


, DST ( ). , , (America/Sao_Paulo).

- DST 16 th 2016: 1 1 ( -03:00 -02:00). , 00:00 00:59 ( , 23: 59: 59.999999999 01:00). , :

ZoneId zone = ZoneId.of("America/Sao_Paulo");

// October 16th 2016 at midnight, DST started in Sao Paulo
LocalDateTime d = LocalDateTime.of(2016, 10, 16, 0, 0, 0, 0);
ZonedDateTime z = d.atZone(zone);
System.out.println(z);// adjusted to 2017-10-15T01:00-02:00[America/Sao_Paulo]

DST : 19 th 2017 1 , 23 18 th ( -02:00 -03:00). , 23:00 23:59 ( : -03:00 -02:00), , . DST, withLaterOffsetAtOverlap() DST:

// February 19th 2017 at midnight, DST ends in Sao Paulo
// local times from 23:00 to 23:59 at 18th exist twice
LocalDateTime d = LocalDateTime.of(2017, 2, 18, 23, 0, 0, 0);
// by default, it gets the offset before DST ends
ZonedDateTime beforeDST = d.atZone(zone);
System.out.println(beforeDST); // before DST end: 2018-02-17T23:00-02:00[America/Sao_Paulo]

// get the offset after DST ends
ZonedDateTime afterDST = beforeDST.withLaterOffsetAtOverlap();
System.out.println(afterDST); // after DST end: 2018-02-17T23:00-03:00[America/Sao_Paulo]

, DST (-02:00 -03:00). epochMilli.

, with.

+6

:

/**
 * Returns the time range between the midnight and current time in milliseconds.
 *
 * @param zoneId time zone ID.
 * @return a {@code long} array, where at index: 0 - midnight time; 1 - current time.
 */
public static long[] todayDateRange(ZoneId zoneId) {
    long[] toReturn = new long[2];

    //ZonedDateTime nowZdt = LocalDateTime.now().atZone(zoneId);
    ZonedDateTime nowZdt = ZonedDateTime.now(zoneId);//As suggested by Hugo (tested). 
    ZonedDateTime midZdt = nowZdt.with(LocalTime.MIDNIGHT);
    toReturn[0] = midZdt.toInstant().toEpochMilli();
    toReturn[1] = nowZdt.toInstant().toEpochMilli();
    return toReturn;
}
+1

Source: https://habr.com/ru/post/1684058/


All Articles