refine Date as start of the day via LocalDateTime - Java java.time

Java examples for java.time:LocalDateTime

Description

refine Date as start of the day via LocalDateTime

Demo Code


//package com.java2s;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Date;
import static java.time.LocalDateTime.ofInstant;
import static java.time.ZoneId.systemDefault;

public class Main {
    public static Date refineDate(Date date) {
        Instant instant = Instant.ofEpochMilli((date).getTime());
        LocalDate localDate = ofInstant(instant, systemDefault())
                .toLocalDate();/*  w w w  .  j  a v a2 s . co m*/

        return java.util.Date.from(localDate.atStartOfDay()
                .atZone(systemDefault()).toInstant());
    }

    public static LocalDate toLocalDate(Date date) {
        Instant instant = Instant.ofEpochMilli((date).getTime());
        return ofInstant(instant, systemDefault()).toLocalDate();
    }
}

Related Tutorials