0

I have problem with java Date type. Why in my code days and days1 both equall 88? Since dateNow1 is the day after dateNow, I had expected days1 to be 1 greater than days.

GregorianCalendar dateNow = new GregorianCalendar(2014,03,31); GregorianCalendar dateFirstDay = new GregorianCalendar(2014,01,01); long diffInMillies = dateNow. getTimeInMillis() - dateFirstDay. getTimeInMillis(); int days = (int) (diffInMillies / (1000*60*60*24)); GregorianCalendar dateNow1 = new GregorianCalendar(2014,04,01); GregorianCalendar dateFirstDay1 = new GregorianCalendar(2014,01,01); long diffInMillies1 = dateNow1. getTimeInMillis() - dateFirstDay1. getTimeInMillis(); int days1 = (int) (diffInMillies1 / (1000*60*60*24)); 
3
  • 2
    (As an aside, you realize you're finding the difference between "April 31st" and February 1st, right? Months are 0-based in Calendar...) Commented Sep 7, 2015 at 14:22
  • Use java.time, the modern Java date and time API since Java 8, for all of your date work. You are demonstrating quite nicely that one cannot work reliably with GregorianCalendar. Commented Aug 1 at 6:26
  • 1
    maybe an odd behaviour, but as specified/documented -- one more reason to stop using these classes and switch to the java.time ones Commented Aug 1 at 6:37

3 Answers 3

3

The reason is that you are giving dateNow a month of 03, which means it is taking it as April, since it starts the month from 0. 0=Jan;1=Feb etc. Now, you are giving day of month as 31. Since April only has 30 days, it is incrementing and treating the date as 1 May, which is same as your dateNow1.

Hence the same values.

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Is day's zero based too?
3

It's the expected behavior, infact:

GregorianCalendar dateNow = new GregorianCalendar(2014,03,31); 

represents the same date as

GregorianCalendar dateNow1 = new GregorianCalendar(2014,04,01); 

This is because months count start from 0, so 03 is April and not March. Visit Official GregorianCalendar JavaDoc for more info on constructors usage and other.

2 Comments

Is day's zero based too?
No, DAY_OF_MONTH starts from 1
1

Octal numbers

(2014,03,31)

Beware: Prefixing a numeric literal in Java means octal base-8 numbers rather than decimal base-10 numbers. Drop that leading zero: (2014,3,31)

Avoid legacy date-time classes

Update for modern Java: Avoid using the terribly flawed date-time classes from the early days of Java. These have been supplanted by the java.time classes built into Java 8+.

java.time

LocalDate

For a date-only value, use java.time.LocalDate class.

LocalDate begin = LocalDate.of( 2014 , 3 , 31 ) ; 

ChronoUnit.DAYS

If you just want the number of days between two dates, just use the ChronoUnit.DAYS enum with its convenient between method. Note the result is a long, not int.

long days = ChronoUnit.DAYS.between( begin , end ) ; 

Milliseconds

Your Question asked about milliseconds. I suspect that is only because of the feebleness of the legacy classes. But just in case…

ZoneId

To count millisecond, your need the first moment of the day. Determining first moment requires a time zone. The day starts many hours earlier in Tokyo Japan than in Toulouse France, and even more hours later in Toledo Ohio US.

ZoneId z = ZoneId.of( "Asia/Tehran" ) ; 

ZonedDateTime

Always let java.time determine the first moment. Never assume the day starts at 00:00. Some days on some dates in some zones start at another time, such as 01:00.

ZonedDateTime zdtBegin = begin.atStartOfDay( z ) ; 

Do the same for the end.

Duration

Calculate the elapsed time on a scale of hours, minutes, seconds, and nanoseconds with the class java.time.Duration.

Duration duration = Duration.between( zdtBegin , zdtEnd ) ; 

To get a count of milliseconds while ignoring any microseconds and nanoseconds, call toMillis.

long millis = duration.toMillis() ; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.