Reputation: 335
For my college project(hotel management system), I need to calculate difference between two dates, I worked simply with followed code.
txtarrivaldate.getDate();
txtdeparturedate.getDate();
long arrivalmillisec = txtarrivaldate.getDate().getTime();
// System.out.println(arrivalmillisec);
long departuremillisec = txtdeparturedate.getDate().getTime();
// System.out.println(departuremillisec);
long diffmilisec = (departuremillisec - arrivalmillisec);
long diffdays = (departuremillisec - arrivalmillisec) / (24 * 60 * 60 * 1000);
String noofdays = Long.toString(diffdays);
System.out.println(noofdays);
However I came across the other java projects and they had the code as
static final long ONE_HOUR = 60 * 60 * 1000L;
dt1=jDateChooser1.getDate();
dt2=jDateChooser2.getDate();
String strdtver1=(String) sdf.format(jDateChooser1.getDate());
String strdtver2=(String) sdf.format(jDateChooser2.getDate());
long diff=((dt2.getTime()-dt1.getTime()+ONE_HOUR)/(ONE_HOUR*24));
diffday=Long.toString(diff);
jTextField4.setText(diffday+" day(s)");
I couldnot get why ONE_HOUR has been added in between two dates. I would eagerly like to thank for sorting the confusion.
Upvotes: 1
Views: 213
Reputation: 338181
The Answer by Mehta is correct.
Also, you are entirely using the wrong classes.
The troublesome old date-time classes (Date
, Calendar
, etc.) are now legacy, supplanted by the java.time classes.
Instant
Instead of java.util.Date
use Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
If you need to interface with old code not yet updated to java.time, convert. Look to new methods added to the old classes.
Instant instant = myJavaUtilDate.toInstant();
Generally best to work in UTC, especially for data storage and data exchange. So make frequent use of the Instant
class.
For determining days, we need to determine dates. And for determining dates, we need time zone. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight Paris France is a new day while still being “yesterday” in Montréal Québec.
ZonedDateTime
Apply a time zone (ZoneId
) to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone();
ChronoUnit
Do not try to do the date-time math yourself. To begin with, you are assuming every hour is sixty minutes, every day is 24 hours, and so on. You would be wrong. Anomalies such as Daylight Saving Time (DST) and politicians frequently re-defining time zones mean such assumptions are wrong. Let java.time do the math.
You seem to want a number of days between two points in time. For that, use the ChronoUnit
enum, an implementation of TemporalUnit
. When fed ZonedDateTime
objects, the ChronoUnit
class is savvy with time zones and anomalies such as DST change-over. Be sure to read the class doc to know and understand its behavior in such situations.
long days = ChronoUnit.DAYS.between( zdtStart , zdtStop );
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 0
Reputation: 30809
That's probably because of timezone
difference (UTC vs BST or something). Second approach is trying to fix it by adding an hour to end date.
I would recommend setting the default
timezone in the application startup and let Java
handle the timezone
conversion, e.g.:
public static void main(String[] args) throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = sdf.parse("2017-01-20");
Date d2 = sdf.parse("2017-01-28");
long diffdays = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);
System.out.println(diffdays);
}
Another approach would be to use Java 8
's Date-Time libraries to calculate the days, e.g.:
public static void main(String[] args) throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = sdf.parse("2017-01-20");
Date d2 = sdf.parse("2017-01-28");
long days = Duration.between(d1.toInstant(), d2.toInstant()).toDays();
System.out.println(days);
}
Here's the javadoc for Duration
class.
Upvotes: 4