• Post Reply Bookmark Topic Watch Topic
  • New Topic

xsd:DateTime to timestamp in java  RSS feed

 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, can anyone suggest me to resolve the problem i am stuck with.Below is the code snippet which is g
iving me parseException .

value coming from xml is <CreatedTS>2006-05-04T18:13:51.0Z</CreatedTS

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
java.util.Date parsedDate = dateFormat.parse(e.getChild("CreatedTS",doc.getRootElement().getNamespace()).getValue());
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
dataSyncBean.setCreatedTs(timestamp);

Thanks-Arun>
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exceprion which i am getting is

java.text.ParseException: Unparseable date: "2006-05-04T18:13:51.0Z"
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your format string is wrong. Note that "hh" means 12-hour clock. You most likely want "HH", 24-hour clock. Also, you should use "X" instead of "Z" for ISO-8601 timezones. (Note that "X" will only work if you are using Java 7 or newer). So your format string should look like this:

"yyyy-MM-dd'T'HH:mm:ss.SSSX"

An alternative way to parse ISO-8601 format dates and times (which is the standard format that is used in XML for dates and times) is this:
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper .It worked .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!