This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Comparing dates... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Comparing dates..." Watch "Comparing dates..." New topic
Author

Comparing dates...

Daniel Harris
Greenhorn

Joined: Feb 22, 2009
Posts: 5
If I subtract a date that is recent (a few weeks) from today's date, I get a negative number returned.



getTimeInMillis() returns a negative number if the date of entryDate is within the past few weeks.

Can anybody tell me why?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

It should only return a negative value if the date is before 00:00:00 GMT on January 1st 1970. This date time is also called the epoch - see java.util.Calendar for more info. This point of time is represented as 0 in Java's calendars and dates (among others; PHP also uses it). Any time before that is always negative.

That still does not explain why a few weeks ago is negative. Can you tell us what values you are using when creating the GregorianCalendar?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Daniel Harris
Greenhorn

Joined: Feb 22, 2009
Posts: 5
Thanks or replying!

Heres an example that returns a negative number...


then == a negative number.

Like I said though... it only does this if the past date is within a few weeks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16479
    
    2

But that's not a "past date". It's March 23 of this year and we haven't got there yet.
Daniel Harris
Greenhorn

Joined: Feb 22, 2009
Posts: 5
Paul Clapham wrote:But that's not a "past date". It's March 23 of this year and we haven't got there yet.



Ahhh.... I see says the greenhorn! Thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

This is exactly why you can better use the Calendar constants like Calendar.FEBRUARY.
Daniel Harris
Greenhorn

Joined: Feb 22, 2009
Posts: 5
Rob Prime wrote:This is exactly why you can better use the Calendar constants like Calendar.FEBRUARY.


The calendar is created with variables... The constants dont apply here. Thanks for the advice though!
 
jQuery in Action, 2nd edition
 
subject: Comparing dates...
 
Similar Threads
Number of weeks between two dates
Using Date in servlet
One more GregorianCalendar question
Adding a day to a java.util.date
typecast gregoriancalendar object as date object?