Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

date

I have a variable of the type java.util.Date.

How can I set the time part to 00:00:00?

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

like image 391
Anthony Kong Avatar asked Dec 06 '13 01:12

Anthony Kong


People also ask

How do I change my date to 00 00 00 in Java?

set(Calendar. MINUTE, 0); calendar.

How do I change the TimeZone in Java Util date?

You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.

Does Java Util date have time?

No time data is kept. In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java. util.


2 Answers

To remove time from the Date object completely, you could 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(); 
    }

Pass into the method the Date object that you want to modify and it will return a Date that has no hours/minutes etc.

If changing the Date object itself isn't required, use SimpleDateFormat. Set the format the way you want ie. remove hours/minutes. Then call the format method and pass in the Date object you want changed.

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy");
System.out.println(sdf.format(yourDate));
like image 183
Sionnach733 Avatar answered Oct 19 '22 05:10

Sionnach733


tl;dr

If you meant midnight in UTC in an 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, discard the legacy Date portion above and just use the 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 in a particular time zone rather than UTC…

LocalDate.now()
         .atStartOfDay()

Better to specify your desired/expected time zone than rely implicitly on current default.

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

Details

Caution: Not every day in every time zone has a time of 00:00:00. Daylight Savings Time can jump to 1 AM, eliminating a midnight.

java.time

Using java.time classes that supplant the troublesome old legacy date-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 );

Convert to/from legacy classes

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

Calendar myLegacyCal = GregorianCalendar.from( zdt ) ;

Date myLegacyDate = Date.from( zdt.toInstant() ) ;

If you want a java.util.Date, which is always in UTC time zone, to have a time-of-day of 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 supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


Joda-Time

UPDATE: The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes. This section left intact as history.

Use Joda-Time 2.3, with a method for this very 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 );

When run…

now: 2013-12-05T23:00:23.785-08:00
firstMomentOfToday: 2013-12-05T00:00:00.000-08:00
like image 22
Basil Bourque Avatar answered Oct 19 '22 03:10

Basil Bourque