English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

are there any functions in comparing and subtracting Dates in Java

example
june 10,2007
- june 5, 2007
____________
5 days

2007-07-29 02:16:46 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

The Calendar class is where most date manipulation is done, but there aren't built-in methods for "number of days from" (another Calendar).

More or less, you have to write it yourself. A couple of ways:

(1) Get the millisecond clock values from two different Calendars, and divide by (1000*60*60*24). This works for most cases, but might get tripped up by the switch to/from Daylight Savings Time (because Midnight July 1 is not an even multiple of 24 hours from Midnight December 1).

(2) Get the day of the year (c.get( Calendar.DAY_OF_YEAR)) from each Calendar, and subtract... but you'll have to code for proper handling of the case where the two Calendars are in different years.

2007-07-29 06:05:11 · answer #1 · answered by McFate 7 · 0 0

You may also want to check out the calendar class

2007-07-29 09:29:05 · answer #2 · answered by AnalProgrammer 7 · 0 0

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html

2007-07-29 09:24:21 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers