🔒 Locked
[java] Date Manipulation
Advertisement
Original Post
What do you mean by add two dates?
You use
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).
codeka.com - Just click it.
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.
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.
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.
This might help:
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
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
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
Well, the API can handle this for you actually!
just do:
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]
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]
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.
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
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
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
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement