1

I am working on dates in Java. I am finding out the difference between two dates.

public static void main(String[] args) {
    final Date futDate = new GregorianCalendar(2012, 8, 15, 0, 0, 0).getTime();

    final Date currentDate = new GregorianCalendar().getTime();

    long diff = Math.round((futDate.getTime() - currentDate.getTime()) / 1000);

    System.out.println(diff / 86400 + " days");
    System.out.println((diff % 86400) / 3600 + " hrs");
    System.out.println(((diff % 86400) % 3600) / 60 + " mins");
    System.out.println((((diff % 86400) % 3600) % 60) % 60 + " secs");
}

Output:

31 days
8 hrs
37 mins
30 secs

Even though the date difference is less than a day, the output is more than 31 days.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
rahul
  • 6,447
  • 3
  • 31
  • 42

5 Answers5

8

8 isn't current month number :)

months are numbered from 0

Java's dates are painful, so I suggest take a look at http://joda-time.sourceforge.net/

dantuch
  • 9,123
  • 6
  • 45
  • 68
  • 1
    And that's why we have constants for this: Calendar.AUGUST (which is 7) http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#AUGUST – Thilo Aug 14 '12 at 09:58
  • oh... Thx! I am using Calendar.AUGUST as suggested by duffymo. – rahul Aug 14 '12 at 09:59
  • @rahul, good for you. remember that days start from 1 and months start from 0. That's just java... ;] – dantuch Aug 14 '12 at 10:00
  • That's not just Java. Many languages/API do it this way (which of course does not make it any better). – Thilo Aug 14 '12 at 10:01
  • 1
    @Thilo I suppose it came from C, and was left that way in Java as well, as it was common way to do it... Anyway, common doesn't mean good. – dantuch Aug 14 '12 at 10:03
  • @Thilo: More *modern* platforms tend to be saner, in my experience. – Jon Skeet Aug 14 '12 at 10:04
5

Better yet, use the built in constants:

Calendar.AUGUST 
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Not that this is really useful, unless you are really hardcoding AUGUST into your code. Normally, you need to parse input data. Of course, DateFormat handles that. – Thilo Aug 14 '12 at 10:00
  • Just answering the question as stated, Thilo. Of course it's not typical. – duffymo Aug 14 '12 at 10:01
3

I am working on dates in Java.

You'd be much better off using Joda Time

I am finding out the difference between two dates.

Joda Time makes this really easy:

LocalDateTime start = ...;
LocalDateTime end = ...;
Period period = new Period(start, end, PeriodType.dayTime());

System.out.println(period.getDays() + " days");
System.out.println(period.getHours() + " hrs");
System.out.println(period.getMinutes() + " mins");
System.out.println(period.getSeconds() + " secs");
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

The month is 0 based as described in the API

So you might want to set it to 7 instead of 8.

Also for duration calculation there is Joda Time a IMO pretty sophisticated time API.

Candlejack
  • 248
  • 1
  • 3
  • 15
0

The accepted Answer by dantuch is correct. The GregorianCalendar class has crazy numbering of months 0-11 for January-December.

Using java.time

The troublesome old date-time classes such as GregorianCalendar are now legacy, supplanted by the modern java.time classes.

Most importantly, the java.time classes use sane numbering for months, 1-12 for January-December. And the Month enum makes working with months even more explicit and less error-prone.

You are ignoring the issue of time zone in determining a date. Your code implicitly uses the JVM’s current default time zone. That default can change at any time by a call from any code in any thread of any app within the JVM. Better to be explicit about your desired/expected time zone.

Also, you assume the day starts at the time 00:00:00. That is not always true because of anomalies such as Daylight Saving Time (DST).

LocalDate ld = LocalDate.of( 2012 , 8 , 15 );

…or…

LocalDate ld = LocalDate.of( 2012 , Month.AUGUST , 15 );

Let java.time determine the first moment of the day.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtThen = ld.atStartOfDay( z );

Get the current moment.

ZonedDateTime zdtNow = ZonedDateTime.now( z );

Get elapsed time with the Duration class for days-hours-minutes-seconds or Period for years-months-days.

Duration d = Duration.between( zdtThen , zdtNow );

No automatic way to build your desired string. In Java 9 and later you can extract the parts to build your own string: toDaysPart, toHoursPart, toMinutesPart, and toSecondsPart.


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?

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154