*
The moose likes Beginning Java and the fly likes Date calculation 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 "Date calculation" Watch "Date calculation" New topic
Author

Date calculation

auvrm papu
Ranch Hand

Joined: Sep 01, 2005
Posts: 105
How to find date ( probably in long) of 7 days before today?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Check out java.util.Calendar.

Or simply do some math on the long value.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
auvrm papu
Ranch Hand

Joined: Sep 01, 2005
Posts: 105
Thanks Bear,
Can someone please elaborate on how do I go about doing calculations in Long?

For Ex: 1227306791752
this should pretty much represent Nov 21, 2008 around 4.33 pm

But I don't understand how those numbers are represented
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

But I don't understand how those numbers are represented


Well, it's your data!! You have to tell us how it is represented !! Different systems have different representations.


Now, if this is the same as the long that is returned from the System clas, then the value is "the difference, measured in milliseconds, between the [represented] time and midnight, January 1, 1970 UTC".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
auvrm papu
Ranch Hand

Joined: Sep 01, 2005
Posts: 105
I used the following

[ November 21, 2008: Message edited by: Ais Kaly ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Ais Kaly:
I used the following



That's the same representation as the System class. It's the current time, measured in milliseconds, from January 1st, 1970 UTC.

So, to subtract a week, just subtract a week's worth of milliseconds.

Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

I object!

Usually subtracting a week's worth of milliseconds is not a problem. However, there is this nasty thing called "daylight savings time", that once a year makes a day 23 hours and once a year 25 hours. This makes the calculation inaccurate if the hours is before 1 am or after 11 pm.

I'll second Bear's recommendation of Calendar:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date calculation