Groovy web console

subscribe to the feed Subscribe
to this
site

DateTime20mins

Published 2 months ago by Marcello de Sales with tags 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 void 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);
    println ("Time is ${dateTime} and grace period is ${dateTime.plusMinutes(20)}")
    println ("Morning ${earlyMorning} and night ${lateNight}")
}

getDateZeroHoraMidnightInterval(getLocalDateTime(1628026655000))