Skip to main content
gamedev.net
🔒 Locked

[java] Date Manipulation

Started by NuffSaid Jan 25, 2003 at 7:06 AM 10 replies 600+ views
Original Post
NuffSaid
NuffSaid
I need to add 2 dates together in Java. How do I go about that? I''ve looked at the Date class, but useful methods like getYear(), getDay(), etc are deprecated, so I''d better not use them. GregorianCalendar looks promising, but I can''t really subtract/add 2 dates, can I?
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
markuskidd
markuskidd
What do you mean by add two dates?
Star Dart - Java Multiplayer Space Combat at http://www.stardart.net/
NuffSaid
NuffSaid
Hmm.... actually, I don''t need to add 2 dates. My bad. What I really need is a way to subtract 2 dates, i.e. get the difference in days/months/years between 2 dates.

Any help would be great.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Dean Harding
Dean Harding
You use java.util.Calendar, specifically the add method, like so:

Calendar currentDate = Calendar.getInstance();
Calendar twoDaysAgo = currentDate.add( Calendar.DATE, -2 );


The reason for not putting these methods in the Date class is because you want to be able to represent dates using different calendars (like Gregorian, or whatever they''ll support in the future).

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

NuffSaid
NuffSaid
Dean, thanks for the info, but that isn''t really what I''m looking for.

Let''s say I have 2 different dates, and I want to know the difference between them. Say I have a Calendar which has the date set to 23 March 2003. I''d like to calculate how many days off that date is from now.

At the moment, the only way I can see it is to do a manual calculation, which for a lazy person like me, sucks. I was hoping there is a way to do that in the Java classes, but I guess I might be wrong.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Ditto Brother Rat
Ditto Brother Rat
This might help:


private static final long SECONDS_PER_WEEK = 60 * 60 * 24 * 7;
private static final long SECONDS_PER_DAY = 60 * 60 * 24;
private static final long SECONDS_PER_HOUR = 60 * 60;
private static final long SECONDS_PER_MINUTE = 60;

long delta = (System.currentTimeMillis() - date) / 1000;
long weeks = delta / SECONDS_PER_WEEK;
long days = (delta -= weeks * SECONDS_PER_WEEK) / (SECONDS_PER_DAY);
long hours = (delta -= days * SECONDS_PER_DAY) / (SECONDS_PER_HOUR);
long minutes = (delta -= hours * SECONDS_PER_HOUR) / SECONDS_PER_MINUTE;
long seconds = delta - minutes * SECONDS_PER_MINUTE;


It''s been a while since I used it so I can''t really remember how it works. Something to help get you started anyway
markuskidd
markuskidd
The best answer lies within the Calendar-related classes. Just look in the API and you will find something, I''m sure. If you want a pretty iffy way of doing it that is off the top of my head, here you go:


Date firstDate = new Date(); // set to current time
... blah blah ...
Date secondDate = new Date(); // set to current time
... blah blah ...

long differenceInTime = secondDate.getTime() - firstDate.getTime();
int daysDifference = (differenceInTime / 1000 / 60 / 24) + 1; // milliseconds converted to days, rounded up
Star Dart - Java Multiplayer Space Combat at http://www.stardart.net/
NuffSaid
NuffSaid
I was afraid that it''d come to that. Oh well, writing my own routine isn''t that hard. I was just hoping that the API would have done it for me
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Petr Stedry
Petr Stedry
Well, the API can handle this for you actually!

just do:

  
public static Date add(Date date, Date date2) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
GregorianCalendar cal2 = new GregorianCalendar();
cal2.setTime(date2);

// modify the Date inside the "cal" as this will be returned

// it is enough to modify the DAY_OF_YEAR and YEAR fields

// if what you're looking for is just date addition

cal.set(GregorianCalendar.DAY_OF_YEAR,cal.get(GregorianCalendar.DAY_OF_YEAR) + cal2.get(GregorianCalendar.DAY_OF_YEAR) );
cal.set(GregorianCalendar.YEAR,cal.get(GregorianCalendar.YEAR) + cal2.get(GregorianCalendar.YEAR) );
// else you can similarly process the following fields

// GregorianCalendar.HOUR_OF_DAY

// GregorianCalendar.MINUTE

// GregorianCalendar.SECOND

// GregorianCalendar.MILLISECOND


// return the resulting date

return cal.getTime();
}

have fun

Petr Stedry

EDIT> chanded code tags to source tags (the code was too wide )

[edited by - Petr Stedry on January 27, 2003 5:52:30 AM]
Petr Stedry
markuskidd
markuskidd
GregorianCalendar can solve this problem. It''ll be up to you though to read the docs and learn how to get the difference between two dates though.
Star Dart - Java Multiplayer Space Combat at http://www.stardart.net/
NuffSaid
NuffSaid
quote:
Original post by markuskidd
GregorianCalendar can solve this problem. It''ll be up to you though to read the docs and learn how to get the difference between two dates though.


Really? Thank you so much for telling me that. I wouldn''t have guessed it myself. You''ve added greatly to my understanding.

Petr: Thanks for the code example. But I''ve already solved my problem I''m using the milisecond approach, and it works. Too bad the Calendar class doesn''t have a method like diff(Calendar, int option) that returns the difference between 2 dates in days/months/years.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Petr Stedry
Petr Stedry
Hi,

The millisecond approach is ok IMO. When computing the difference between two dates, the result is a relative difference, that can occur between many dates. So the calendar approach does not make much sense in this case - it does not add any information. Treating a date difference as an actual date does not make much sense unless you are travelling back through time I guess

*smile*


Petr Stedry
Petr Stedry

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.