0

I want to round a java.util.Date object to the end of the day, e.g. rounding 2016-04-21T10:28:18.109Z to 2016-04-22T00:00:00.000Z.

I saw Java Date rounding, but wasn't able to find something compareable for the end of the day. It also is not the same as how to create a Java Date object of midnight today and midnight tomorrow?, because I don't want to create a new Date (midnight today or tomorrow), but the next midnight based on any given date.

Community
  • 1
  • 1
Yanick Nedderhoff
  • 1,174
  • 3
  • 18
  • 35
  • 1
    But why? Apparenly you are using Java 8 so why not just use the new time API? – Tunaki Apr 21 '16 at 10:47
  • Because it's a very large project where I don't get to decide to use that. – Yanick Nedderhoff Apr 21 '16 at 10:48
  • 5
    So you can use 3rd party APIs but can't use the built-in JDK APIs? This is usually the other way around. – Tunaki Apr 21 '16 at 10:49
  • 1
    End of day in which time zone? You've given an example in UTC, but are you sure that's what you want? And why are you asserting one particular way of doing it (DateUtils) rather than just the effect you want to achieve? If I give an answer that doesn't use DateUtils, is that a problem? – Jon Skeet Apr 21 '16 at 10:49
  • Ok, I adjusted my question. I don't need to use `DateUtils`, that didn't make sense. – Yanick Nedderhoff Apr 21 '16 at 10:50
  • "I saw this, but wasn't able to find something compareable for the end of the day." - If you can round "down" then you can just add a day and you're done. Or am I missing something? – Fildor Apr 21 '16 at 10:52
  • 2
    2016-04-22T00:00:00.000Z - are you sure that this is _end_ of day 2016-04-21T10:28:18.109Z, why not 2016-04-21T23:59:59.999Z? – Mikhail Kuchma Apr 21 '16 at 10:52
  • 3
    Take a look at http://stackoverflow.com/questions/6850874/how-to-create-a-java-date-object-of-midnight-today-and-midnight-tomorrow – eol Apr 21 '16 at 10:53
  • 1
    Sure. When you have a `LocalDateTime` for example, you can call `.with(LocalTime.MAX);` and it's done. – Tunaki Apr 21 '16 at 10:56

3 Answers3

2

The DateUtils.ceiling serves your purpose. Pass Calendar.DATE for field value.

Adisesha
  • 5,200
  • 1
  • 32
  • 43
1

Given the documentation of DateUtils, I'm not sure I'd trust it with this.

Assuming you're only interested in a UTC day, you can take advantage of the fact that the Unix epoch is on a date boundary:

public static Date roundUpUtcDate(Date date) {
    long millisPerDay = TimeUnit.DAYS.toMillis(1);
    long inputMillis = date.getTime();
    long daysRoundedUp = (inputMillis + (millisPerDay - 1)) / millisPerDay;
    return new Date(daysRoundedUp * millisPerDay);
}

I would strongly urge you to move to the java.time API if you possibly can though.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Traditional way

@Test
public void testDateRound() throws ParseException {
    Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse("2016-04-21T10:28:18.109Z");
    System.out.println(date);
    Calendar cl = Calendar.getInstance();
    cl.setTime(date);
    cl.set(Calendar.HOUR_OF_DAY, 23);
    cl.set(Calendar.MINUTE, 59);
    cl.set(Calendar.SECOND, 59);
    cl.set(Calendar.MILLISECOND, 999);
    System.out.println(cl.getTime());
    cl.add(Calendar.MILLISECOND, 1);
    System.out.println(cl.getTime());
}

Output

Thu Apr 21 10:28:18 GMT+03:00 2016
Thu Apr 21 23:59:59 GMT+03:00 2016
Fri Apr 22 00:00:00 GMT+03:00 2016
Mikhail Kuchma
  • 583
  • 2
  • 9