4

What's the most efficient way to remove the time portion from a Java date object using only Classes from within the JDK?

I have the following

myObject.getDate() = {java.util.Date}"Wed May 26 23:59:00 BST 2010"

To reset the time back to 00:00:00, I'm doing the following

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date myDate = sdf.parse(sdf.format(myObject.getDate()));

The output is now

myDate = {java.util.Date}"Wed May 26 00:00:00 BST 2010"

Is there a better way to achieve the same result?

Pram
  • 2,261
  • 3
  • 31
  • 50
  • Consider JODA Time. http://joda-time.sourceforge.net/ ; much more pleasant to work with. Also http://stackoverflow.com/questions/2901262/why-were-most-java-util-date-methods-deprecated – polygenelubricants Aug 25 '10 at 13:17
  • Not an option for me I'm afraid. I'm working with a third party app that won't allow for additional jars to be added. – Pram Aug 25 '10 at 13:19

5 Answers5

10

More verbose, but probably more efficient:

    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(date);
    // cal.set(Calendar.HOUR, 0);   // As jarnbjo pointed out this isn't enough
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

Also, you don't need to worry about locale settings, which may cause problems with string to date conversions.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • I've just tried this out and I get the following output myDate = {java.util.Date}"Wed May 26 12:00:00 BST 2010" – Pram Aug 25 '10 at 13:55
  • Are you perhaps formatting the date in 12h instead of a 24h setting? – kgiannakakis Aug 25 '10 at 14:05
  • I'm getting a Date object out of cal by calling cal.getTime() – Pram Aug 25 '10 at 14:44
  • 1
    `GregorianCalendar.getInstance();` does nothing other than `Calendar.getInstance();`. It's a static method of the Calendar class, this is just an unnecessary import. – Sean Patrick Floyd Aug 25 '10 at 15:03
  • 5
    You must use cal.set(Calendar.HOUR_OF_DAY, 0). Calendar.HOUR is just the 12-hour-field of the calendar and will leave the AM_PM field unchanged, causing the 24-hour-field to be set to either 0 or 12. – jarnbjo Aug 25 '10 at 15:10
  • `GregorianCalendar.getInstance();` is not the same as `Calendar.getInstance();`. The later takes the current timezone and locale into account and may return something different than GegorianCalendar. – whiskeysierra Aug 26 '10 at 07:00
  • @whiskeysierra, `GregorianCalendar` doesn't declare any `getInstance` methods, so `GregorianCalendar.getInstance()` is just an odd way of invoking `Calendar.getInstance()`. In my opinion these "indirect" static invocations shouldn't have be allowed and should be avoided. – Daniel Lubarov Nov 27 '13 at 03:16
8

If you have Apache commons, you can use DateUtils.truncate():

Date myDate = DateUtils.truncate(myObject.getDate(), Calendar.DATE)

(If you don't have access to Apache Commons, DateUtils.truncate() is implemented basically the same as kgiannakakis's answer.)


Now, if you want "clever" code that is very fast, and you don't mind using deprecated functions from java.util.Date, here is another solution. (Disclaimer: I wouldn't use this code myself. But I have tested it and it works, even on days when DST starts/ends.)

long ts = myObject.getDate().getTime() - myObject.getDate().getTimezoneOffset()*60000L;
Date myDate = new Date(ts - ts % (3600000L*24L));
myDate.setTime(myDate.getTime() + myDate.getTimezoneOffset()*60000L);
Community
  • 1
  • 1
Jenni
  • 1,668
  • 4
  • 20
  • 28
1

These methods are deprecated, but given a Date, you can do something like this:

Date d = ...;
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);

You should use a Calendar if possible. You'd then use the cal.set(Calendar.MINUTE, 0), etc.

It should be mentioned that if it's at all an option, you should use Joda-Time.

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
0

Are you taking time zone into consideration? I see you have BST right now, but what when BST is over? Do you still wish to use BST?

Anyway, I'd suggest you have a look at DateMidnight from JodaTime and use that.

Things may vary depending on how you want to handle the time zones. But in it's simplest form, it should be as simple as:

DateMidnight d = new DateMidnight(myObject.getDate());

If you must convert back go java.util.Date:

Date myDate = d.toDate();
Jack Leow
  • 21,945
  • 4
  • 50
  • 55
0
myDate.setTime(myDate.getTime()-myDate.getTime()%86400000)

might do the trick. Talk about quick&dirty.

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102