Java Instant get day start millisecond

Description

Java Instant get day start millisecond

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    long l = getDayHeadMills(new Date().getTime());
    System.out.println(l);/*  w w w.ja  va 2s  .  c  o  m*/
  }

  public static long getDayHeadMills(long timestamp) {
    Instant instant = Instant.ofEpochMilli(timestamp);
    ZonedDateTime dateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());

    ZonedDateTime dateHead = ZonedDateTime.of(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth(), 0,
        0, 0, 0, ZoneId.systemDefault());
    return dateHead.toInstant().toEpochMilli();
  }
}



PreviousNext

Related