aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java - Date conversion to milliseconds problem" Watch "Java - Date conversion to milliseconds problem" New topic
Author

Java - Date conversion to milliseconds problem

Balaji H A
Greenhorn

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
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
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?
Bill
Balaji H A
Greenhorn

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:

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, ":");
try
{
while(dateToken.hasMoreTokens())
{
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());
cal.set(year,month-1,day,hr,min,sec);
return cal.getTime();
}
}
catch (Exception e)
{
print(e,"error");
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
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
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.
Bill
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.

Jules
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java - Date conversion to milliseconds problem