• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java - Date conversion to milliseconds problem

 
Balaji H A
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic