Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-11-2007, 02:18 AM
orchid's Avatar
Member
 
Join Date: Apr 2007
Location: Midwest
Posts: 60
orchid is on a distinguished road
Date math
Am new to java and having an issue trying to do some date math.
I made this test code and it returns a date difference of 24 days which is what I wanted. My question is this the correct way to subtract dates or should I be doing something more sophisticated?
Code:
private void playWithDates() { Calendar aDay = new GregorianCalendar(); aDay.set(2007, Calendar.JANUARY, 25, 0, 0, 0); Calendar newYearsDay = new GregorianCalendar(); newYearsDay.set(2007, Calendar.JANUARY, 1, 0, 0, 0); long nyd=newYearsDay.getTimeInMillis(); long a=aDay.getTimeInMillis(); long diff =a-nyd; System.out.println(diff/1000/60/60/24); }
output is: 24
thx
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-11-2007, 02:39 AM
Member
 
Join Date: Apr 2007
Location: USA
Posts: 50
derrickD is on a distinguished road
Well that does work so I'd say if it meets your needs then go for it.
There is a very large and involved discussion on date math that you might want to read here:
java date math
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-18-2007, 08:01 AM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
Calendar class helps you to achieve this with the help roll method,
The description is as below from javadoc of sun
Code:
roll public void roll(int field, int amount) Add to field a signed amount without changing larger fields. A negative roll amount means to subtract from field without changing larger fields. Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling roll(Calendar.MONTH, 8) sets the calendar to April 30, 1999. Using a GregorianCalendar, the DAY_OF_MONTH field cannot be 31 in the month April. DAY_OF_MONTH is set to the closest possible value, 30. The YEAR field maintains the value of 1999 because it is a larger field than MONTH. Example: Consider a GregorianCalendar originally set to Sunday June 6, 1999. Calling roll(Calendar.WEEK_OF_MONTH, -1) sets the calendar to Tuesday June 1, 1999, whereas calling add(Calendar.WEEK_OF_MONTH, -1) sets the calendar to Sunday May 30, 1999. This is because the roll rule imposes an additional constraint: The MONTH must not change when the WEEK_OF_MONTH is rolled. Taken together with add rule 1, the resultant date must be between Tuesday June 1 and Saturday June 5. According to add rule 2, the DAY_OF_WEEK, an invariant when changing the WEEK_OF_MONTH, is set to Tuesday, the closest possible value to Sunday (where Sunday is the first day of the week). Overrides: roll in class Calendar Parameters: field - the time field. amount - the signed amount to add to field. Since: 1.2 See Also: add(int, int), Calendar.set(int, int)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Math levent Java Tutorials 1 05-12-2008 10:03 AM
Difference between current date and anothe date vijay balusamy New To Java 1 04-16-2008 05:15 PM
Math.Random Java Tip Java Tips 0 11-23-2007 03:09 PM
Help with math in java fernando New To Java 1 08-06-2007 07:05 AM
Math Class ritwik07 New To Java 0 07-17-2007 02:39 PM


All times are GMT +3. The time now is 08:10 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org