Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing dates...  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 21327
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Daniel Harris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 23506
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that's not a "past date". It's March 23 of this year and we haven't got there yet.
 
Daniel Harris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 21327
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is exactly why you can better use the Calendar constants like Calendar.FEBRUARY.
 
Daniel Harris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!