This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java - Date conversion to milliseconds problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java - Date conversion to milliseconds problem" Watch "Java - Date conversion to milliseconds problem" New topic

Java - Date conversion to milliseconds problem

Balaji H A

Joined: Aug 19, 2004
Posts: 3
While converting from a date read from a file containing Date, Month, Year, Hour, Minute, Second, using the Calendar and Date objects with the Locale.UK as the standard local for all operations,
there is a mismatch in the milliseconds form of the date when compared to the date format date stored.

Both the milliseconds form of the date and the date form of the date is obtained from the same date object.

Please let me know any bugs faced in the conversion of Date to milliseconds using Date and Calendar object and possible solutions if already known.

Thanks in advance.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12982
I don't see any reason to expect them to match exactly since you only have precision to Second in your file read (if I understand what you mean).
How big is the mismatch?
Balaji H A

Joined: Aug 19, 2004
Posts: 3
Thanks a lot for the reply..

The difference is in the order of about 10 to 13 Days.
The date read from file is converted by the following code:


private Date getDate (String date) throws Exception {
Calendar cal = new GregorianCalendar ();
Date dd = null;
int hr = 0;
int min = 0;
String second = null;
int sec = 0;
int year = 0;
int month = 0;
int day = 0;
StringTokenizer dateToken = new StringTokenizer(date, ":");
hr = Integer.parseInt(dateToken.nextToken().trim());
min = Integer.parseInt(dateToken.nextToken().trim());
second = dateToken.nextToken().trim();
sec = Integer.parseInt(second.substring(0,second.length()-3));
day = Integer.parseInt(dateToken.nextToken().trim());
month = Integer.parseInt(dateToken.nextToken().trim());
year = Integer.parseInt(dateToken.nextToken().trim());
return cal.getTime();
catch (Exception e)
throw new Exception(e.getMessage());
return dd;

// End Of Code

The Calendar object is initialized in the constructor of the class and has been put in the first line of the function to have continuity.

If there is any possible way of improvement to solve the problem, please let me know.
Thank You..
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12982
Wow that is a big discrepency. You might look at Roedy Green's extended discussion of date problems in Java for an inspiration, I can't see anything obviously wrong.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
You'll need to give us some examples for the date string argument. I can see nothing obviously wrong with the code either. You could get that kind of discrepancy, however, if the month and date fields in the input are transposed.

Joe Ess

Joined: Oct 29, 2001
Posts: 9126

I don't know if this will fix your problem, but you are duplicating the functionality of the java.text.DateFormat parse() method. Take a look at it and the subclass SimpleDateFormat.
It's usually better to rely on the API methods for things like this because they are far more extensively tested than your code will be and it will make your code shorter and easier to maintain. DateFormat also has a format() method for converting Date's into Strings.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Java - Date conversion to milliseconds problem
jQuery in Action, 3rd edition