Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Java DateUtils.ceiling and DateUtils.truncate

Tags:

java

datetime

It's not clear in the java doc what the difference between DateUtils.ceiling and DateUtils.truncate is. Is the java doc wrong? Can someone clarify this?

ceiling

public static Date ceiling(Date date, int field)

Ceil this date, leaving the field specified as the most significant field.

For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was passed with MONTH, it would return 1 Mar 2002 0:00:00.000.

vs

truncate

public static Date truncate(Date date, int field)

Truncate this date, leaving the field specified as the most significant field.

For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was passed with MONTH, it would return 1 Mar 2002 0:00:00.000.

like image 309
cmcginty Avatar asked May 14 '12 22:05

cmcginty


People also ask

What is dateutils in Java?

DateUtils (Apache Commons Lang 3.11 API) java.lang.Object. org.apache.commons.lang3.time.DateUtils. public class DateUtils extends Object. A suite of utilities surrounding the use of the Calendar and Date object. DateUtils contains a lot of common methods considering manipulations of Dates or Calendars.

What are the methods in dateutils for date manipulation?

DateUtils contains a lot of common methods considering manipulations of Dates or Calendars. Some methods require some extra explanation. The truncate, ceiling and round methods could be considered the Math.floor (), Math.ceil () or Math.round versions for dates This way date-fields will be ignored in bottom-up order.

What is SQL date in Java?

The java.sql.Date extends java.util.Date class.. Its main purpose is to represent SQL DATE, which keeps years, months and days. 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.

What is the use of date class in Java?

The java.util.Date class represents a particular moment in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time).The class is used to keep coordinated universal time (UTC). We can initialize it in two ways. By calling the constructor: Date date = new Date();


3 Answers

To add on to Jim's answer, I suspect there is a Javadoc error on the ceiling method. The description for ceiling(Date,int) was updated with the 3.0 javadoc (compare to the 2.5 javadoc for the same method)... and although the others weren't updated, that method uses code common to the Calendar version... Or using a simple test case you could see that they both behave the same (for me with 3.1 at least :) )

@Test
public void testCeil() {
    final Calendar date = new GregorianCalendar();
    date.clear();
    date.set(2002, 3, 28, 13, 45, 01);

    System.out.println(date.getTime());
    System.out.println(DateUtils.ceiling(date, Calendar.HOUR).getTime());
    System.out.println(DateUtils.ceiling(date.getTime(), Calendar.HOUR));
    System.out.println(DateUtils.truncate(date, Calendar.HOUR).getTime());
    System.out.println(DateUtils.truncate(date.getTime(), Calendar.HOUR));
    System.out.println(date.getTime());
}
like image 149
Charlie Avatar answered Sep 30 '22 17:09

Charlie


The answer is in the documentation:

The truncate, ceiling and round methods could be considered the Math.floor(), Math.ceil() or Math.round versions for dates This way date-fields will be ignored in bottom-up order.

Which I would interpret as "You're correct, but there is a reason"

like image 23
Jim Barrows Avatar answered Sep 30 '22 17:09

Jim Barrows


The documentation on some of the older versions for the ceil() method is WRONG. It has been corrected at some point, and here's the docs from 3.1:

public static Date ceiling(Date date,
                           int field)
Ceil this date, leaving the field specified as the most significant field.

For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it would return 1 Apr 2002 0:00:00.000.

So, while ceil() and trunc() both minimize all the remaining fields (in some cases sets to 0, but for MONTH it will set the day to 1), ceil() will actually increment the field you pass in by 1, whereas trunc will not.

like image 39
Matt Avatar answered Sep 30 '22 15:09

Matt