Groovy web console

subscribe to the feed Subscribe
to this
site

00:00 and 23:59 of a day

Published 3 months ago by Marcello de Sales with tags date localdatetime
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
import java.time.*;
import java.util.Arrays;
import java.util.TimeZone;

def getMillis(LocalDateTime dateTime) {
    return dateTime.atZone(ZoneId.of("America/Sao_Paulo")).toInstant().toEpochMilli();
}

def LocalDateTime getLocalDateTime(long milliseconds) {    
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), TimeZone.getDefault().toZoneId());
    return dateTime.atZone(ZoneId.of("UTC"))
            .withZoneSameInstant(ZoneId.of("America/Sao_Paulo"))
            .toLocalDateTime();
}

/**
 * @param dateTime
 * @return The start and end hour of the day for searches
 */
def Long[] getDateZeroHoraMidnightInterval(LocalDateTime dateTime) {
    // https://stackoverflow.com/questions/9629636/get-todays-date-in-java-at-midnight-time/31683549#31683549
    LocalDateTime earlyMorning = dateTime.toLocalDate().atStartOfDay();
    LocalDateTime lateNight = earlyMorning.plusHours(23).plusMinutes(59);
    return Arrays.asList(getMillis(earlyMorning), getMillis(lateNight)).toArray(new Long[2]);
}

println getDateZeroHoraMidnightInterval(getLocalDateTime(1627433114788))