File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem or bug with the method getTime() from java.util.Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem or bug with the method getTime() from java.util.Date" Watch "Problem or bug with the method getTime() from java.util.Date" New topic
Author

Problem or bug with the method getTime() from java.util.Date

David Acevedo
Greenhorn

Joined: Feb 12, 2010
Posts: 15
I was coding a method that gets the number of days between two dates. When I was testing the method I found that sometimes it returns a result that differs with the correct answer in 1 day. I check step by step my method and I found this problem.
I don't know what I am doing wrong, but I don't have idea how to solve it, it is too strange.

---------------------
private int getNumberOfDaysBetween(Date date1, Date date2) {
long fechaInicialMs = date1.getTime();
long fechaFinalMs = date2.getTime();
long dif = fechaFinalMs - fechaInicialMs;
double dias = Math.floor(dif / (1000 * 60 * 60 * 24));
return (int) dias;
}

client:

Date test1 = new Date(2010, 2, 24);
Date test2 = new Date(2010, 2, 25);
System.out.println("Result: " + getNumberOfDaysBetween(test2, test1));

With this input it returns 1 that is ok, but when the input is:

Date test1 = new Date(2010, 2, 27);
Date test2 = new Date(2010, 2, 28);
System.out.println("Result: " + getNumberOfDaysBetween(test2, test1));
With this input it returns 0

Then I checked what the getTime method was bringing and I found that for the date 2010/03/28 (Date test1 = new Date(2010, 2, 27)) it brings less milliseconds.
Each day has 86400000 milliseconds (1000*24*60*60) but for that specific date the method getTime() counts just 82800000 then for that reason when I use the method floor() in my method it returns 0.

I printed what getTime returns for different dates in order you appreciate the problem:
Date getTime()
Sat Mar 19 00:00:00 EET 3910 61227093600000
Sun Mar 20 00:00:00 EET 3910 61227180000000
Mon Mar 21 00:00:00 EET 3910 61227266400000
Tue Mar 22 00:00:00 EET 3910 61227352800000
Wed Mar 23 00:00:00 EET 3910 61227439200000
Thu Mar 24 00:00:00 EET 3910 61227525600000
Fri Mar 25 00:00:00 EET 3910 61227612000000
Sat Mar 26 00:00:00 EET 3910 61227698400000
Sun Mar 27 00:00:00 EET 3910 61227784800000
Mon Mar 28 00:00:00 EEST 3910 61227867600000
Tue Mar 29 00:00:00 EEST 3910 61227954000000
Wed Mar 30 00:00:00 EEST 3910 61228040400000
Thu Mar 31 00:00:00 EEST 3910 61228126800000

If you check the difference between each day is exactly 86400000 ms, but with 2010/03/28 it doesn't work.

am I wrong? which one is my mistake??
Maybe I am missing something but I don't have idea what help!!!






Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

David Acevedo wrote:
am I wrong? which one is my mistake??
Maybe I am missing something but I don't have idea what help!!!


Not all days are 24 hours long. For countries that observe "daylight savings", switchover days can be either 23 hours or 25 hours long, depending on the change.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

Welcome to JavaRanch. Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
David Acevedo
Greenhorn

Joined: Feb 12, 2010
Posts: 15
Henry Wong wrote:
David Acevedo wrote:
am I wrong? which one is my mistake??
Maybe I am missing something but I don't have idea what help!!!


Not all days are 24 hours long. For countries that observe "daylight savings", switchover days can be either 23 hours or 25 hours long, depending on the change.

Henry


Thanks a lot!!! Henry.
I am not used to the daylight savings I would never considered that
David Acevedo
Greenhorn

Joined: Feb 12, 2010
Posts: 15
Jesper Young wrote:Welcome to JavaRanch. Please UseCodeTags when you post source code.


Ok Jesper, I will take that into account for the next time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem or bug with the method getTime() from java.util.Date