aspose file tools*
The moose likes Beginning Java and the fly likes Comparing dates... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 19719
    
  20

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 - OCEEJBD 6
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: 18655
    
    8

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: 19719
    
  20

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing dates...