Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easier way to get start of current day time than this?

I basically want to get zero or beginning hour for currrent day.

def today = Calendar.instance
today.set(Calendar.HOUR_OF_DAY, 0)
today.set(Calendar.MINUTE, 0)
today.set(Calendar.SECOND, 0)
println today // Mon Mar 15 00:00:00 SGT 2010
like image 645
Teo Choong Ping Avatar asked Mar 15 '10 09:03

Teo Choong Ping


People also ask

How do you get the day start time?

Use the setUTCHours() method to get the start and end of the day, e.g. startOfDay. setUTCHours(0, 0, 0, 0) . The method takes the hours, minutes, seconds and milliseconds as parameters and sets them for the specified date according to universal time.

Which is the most up to date way to instantiate the current date?

The Clock. systemUTC(). instant() method returns the current date and time both.

How do I get the start of a date in Java?

LocalDateTime startOfDay = LocalDateTime. of(localDate, LocalTime. MIDNIGHT);

How do I get the start and end of a day?

From a LocalDate Object First of all, let's see how we can get the start or end of a day given to us as a LocalDate object, such as: The simplest way of getting a LocalDateTime representing the beginning of a particular day is by using the atStartOfDay () method:

How to get the entire day of a date in UTC?

If you want the entire day of a date as seen in UTC rather than in a time zone, use OffsetDateTime. LocalDate today = LocalDate.now ( ZoneOffset.UTC ) ; OffsetDateTime odtStart = today.atTime ( OffsetTime.MIN ) ; OffsetDateTime odtStop = today.plusDays ( 1 ).atTime ( OffsetTime.MIN ) ;

Is there a way to set the end time at 6pm?

When a shop is open from 8AM to 6PM, the end time displayed is 6PM, not 5:59:59 PM. But there is no built-in method. As commented by @JBNizet, if you want to create an interval, you can also use an interval up to midnight, exclusive. What about daylight saving time? @MateusViccari LocalDate / LocalDateTime have no time zone so no DST rule.

How to get the first moment of the day in Java?

To get the first moment of the day go through the LocalDate class and its atStartOfDay method. ZonedDateTime zdtStart = zdt.toLocalDate ().atStartOfDay ( zoneId ); Using Half-Open approach, get first moment of following day. ZonedDateTime zdtTomorrowStart = zdtStart.plusDays ( 1 );


Video Answer


1 Answers

Not simpler than the other solutions, but less lines:

def now = new GregorianCalendar()
def today = new GregorianCalendar(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH))
println today.getTime()
like image 66
Miklos Csuka Avatar answered Nov 16 '22 04:11

Miklos Csuka