How to get the date and time of the day with LocalDateTime

3

I have this date in the database:

21/07/18 15:52:00,000000000

When the query passes parameter LocalDateTime , I want to list all dates that start the day. How to get the minimum date with LocalDateTime , and make the data query? Remember that you have in the database the milliseconds.

I tried to do the:

 LocalDateTime.Now().MIN

But that way the date is wrong.

    
asked by anonymous 22.08.2018 / 01:38

1 answer

2

MIN is a constant that represents the lowest possible value for a LocalDateTime ( see the documentation ). More precisely, it represents January 1 of the year -999999999 (year "minus 999 million") at midnight. Maybe that's why the date went wrong on your test.

Based on the title of the question ("How to get the date and time of the day with LocalDateTime "): " date and minimum time of day " does not make much sense since a date represents a specific day and can not have a "minimum date of the day".

Finally, I understood that you actually want the "minimum hour of the day" of a given date. That is, midnight of a given day (since the day starts at midnight, and therefore this would be the "minimum hour", ie the lowest possible value for the time of day).

For this you can use the method with passing a LocalTime which corresponds to midnight. This method is useful if you have LocalDateTime any and need to change your time to midnight:

// data e hora atual
LocalDateTime dateTime = LocalDateTime.now();
// mudar horário para meia-noite
LocalDateTime inicioDia = dateTime.with(LocalTime.MIDNIGHT);

But if you need to build an object that corresponds to today and set the time to midnight, you can also do this by using a LocalDate to build the current date, day, and then use the atStartOfDay method, which returns a LocalDateTime with the time set to midnight:

// data de hoje (dia, mês e ano)
LocalDate hoje = LocalDate.now();
// setar horário para meia-noite
LocalDateTime inicioDia = hoje.atStartOfDay();

LocalDate also has the atTime method, which allows you to pass LocalTime to the time you want (if you need anything other than midnight) p>     

22.08.2018 / 03:19