How to set instance java.util.Date time to 00:00:00?

I have a variable like java.util.Date .

How to set the time part at 00:00:00?

I am not allowed to use the Apache Commons or JodaTime library. java.util.Calendar is probably my only option.

+6
source share
3 answers

To completely remove the time from a Date object, you can use this:

 public static Date removeTime(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } 

Go to the method of the Date object you want to change, and it will return a Date that does not have hours / minutes, etc.

If you do not need to change the Date object itself, use SimpleDateFormat . Set the format the way you want, i.e. remove hours / minutes. Then call the format method and pass the Date object that you want to change.

 SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy"); System.out.println(sdf.format(yourDate)); 
+19
source

TL; DR

If you meant midnight in UTC in the old legacy java.util.Date object ...

 java.util.Date.from( OffsetDateTime.of( LocalDate.of( 2017 , Month.JANUARY , 23 ) , LocalTime.MIN , ZoneOffset.UTC ).toInstant() ) 

If possible, drop the previous part of Date above and just use modern java.time.OffsetDateTime or java.time.Instant object ...

 OffsetDateTime.of( LocalDate.of( 2017 , Month.JANUARY , 23 ) , LocalTime.MIN , ZoneOffset.UTC ).toInstant() 

If you meant midnight at a specific time zone , not UTC ...

 LocalDate.now() .atStartOfDay() 

It is better to specify the desired / expected time zone than to implicitly refer to the current default.

 LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) .atStartOfDay( ZoneId.of( "Pacific/Auckland" ) ) 

More details

Attention Not every day in every time zone is 00:00:00. Daylight saving time can go to 1 a.m., excluding midnight.

java.time

Using java.time classes that supersede the nasty old obsolete time classes such as java.util.Date and .Calendar .

 ZoneId z = ZoneId.of( "America/Montreal" ); LocalDate localDate = LocalDate.now( z ); ZonedDateTime zdt = localDate.atStartOfDay( z ); 

Converting to / from obsolete classes

You should stick to modern java.time classes whenever possible. But if you have instances of old heritage classes, you can convert to / from java.time. Look at the new methods added to the old classes .

 Calendar myLegacyCal = GregorianCalendar.from( zdt ) ; Date myLegacyDate = Date.from( zdt.toInstant() ) ; 

If you want java.util.Date , which is always in the UTC time zone, to have the time of day 00:00:00, use OffsetDateTime with the constant ZoneOffset.UTC .

 OffsetDateTime odt = OffsetDateTime.of( localDate , LocalTime.MIN , ZoneOffset.UTC ) ; Date myLegacyDate = Date.from( odt.toInstant() ) ; 

About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy datetime classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .


Joda time

UPDATE: The Joda-Time project is now in maintenance mode. The team advises switching to the java.time classes. This section has remained untouched as a story.

Use Joda-Time 2.3, using a method for this purpose: withTimeAtStartOfDay () .

 // ยฉ 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so. // import org.joda.time.*; DateTime now = new DateTime(); DateTime firstMomentOfToday = now.withTimeAtStartOfDay(); System.out.println( "now: " + now ); System.out.println( "firstMomentOfToday: " + firstMomentOfToday ); 

At startup ...

 now: 2013-12-05T23:00:23.785-08:00 firstMomentOfToday: 2013-12-05T00:00:00.000-08:00 
+2
source

In Java 8 'java.time.LocalDate.atStartOfDay () can also be used:

  Date date = new Date(); Instant inst = date.toInstant(); LocalDate localDate = inst.atZone(ZoneId.systemDefault()).toLocalDate(); Instant dayInst = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); Date day = Date.from(dayInst); 
+1
source

Source: https://habr.com/ru/post/959437/


All Articles