• Post Reply Bookmark Topic Watch Topic
  • New Topic

why I got a ParseException while parsing a String with date and time?  RSS feed

 
patt rich
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a String with the date and time which I want to convert to a date object.

Following is my code:

elementString = "2008-01-29 22:15:00.00Z";
try{
Date elementDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz").parse(elementString);
}catch(ParseException ex){
System.out.println(ex.getMessage());


I get the Exception :Unparseable date: "2008-01-29 22:15:00.00Z"

Could you please help me?
Thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javadocs of the SimpleDateFormat provide details of what a valid pattern looks like. In particular, the "zzz" at the end do not look like they would match ".00Z" (they expect a time zone designation).
 
patt rich
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.
I looked around in the java docs.
When I do not use '00Z', i don't get any exception.
But my time is actually "22:15:00.00Z". And the date is "2008-01-29".

I don't see any way to process "00Z" part of my time.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes - what does the .00Z represent? Hundredths of a second, plus Z for Zulu time? The Z is easy to handle if it's a constant. You need to put single quotes around it so it's not interpreted as a format code, since Z means RFC 822 time zone. Hundredths of a second is harder, since that's not represented by any of the SimpleDateFormat format codes. It's tempting to use milliseconds, but that won't be accurate unless you have three digits. Maybe the millisecond values are always zero anyway? Then you could try:

SimpleDateFormat("yyyy-MM-dd HH:mm:ss.00'Z'")
SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS'Z'")
SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'")

The first of these will give you an error if the hundredths of a second are not zero. The last two will not error, but they will give you inaccurate results if the hundredths arenot zero. For example in a time of 12:34:56.78, the .78 will be interpreted incorrectly as 78 milliseconds, when it should be 780 milliseconds.

If you want accurate handling of the hundredths of a second, the easiest thing would be to simply replace the final Z with a 0 before you parse the string. Then you can treat the last number as milliseconds, and it will be fairly accurate.
 
patt rich
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much Jim. I tried replacing the last 'Z' with 0 and parsed it. it worked.

Thanks Again.
 
patt rich
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am having a new problem with this SimpleDateFormat I have used. if my input is

String accidentDateStringVal = "2006-10-10 12:14:13.00Z";

then

dateConverted = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'")
.parse(accidentDateStringVal);
works fine.

Else if the input format is different like

String accidentDateStringVal = "2006-10-10 12:14.00Z";

then it throws 'Unparseable date' (ParseException).

Is there anyway that I can use a generic format for SimpleDateFormat making sure that I get the time along with the date correctly irrespective of the input?

Thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if it will help in this situation, but you could try to call the SimpleDateFormat.setLenient(true) method first.
 
patt rich
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response. I tried setting "setLenient(true);". But that does not help.

Any other suggestion from anyone please..
Thanks
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try matching your String against several regular expression patterns and choose the date format String accordingly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!