Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Regarding date conversion  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am facing problem with parsing date,means I am getting a date as a string and then parsing it with some specific format like dd:MM:YYYY HH:mm:ss,but this gives exception when the format doesn't match with the one which I am expecting some thing like If I expect dd:MMM:yy HH:mm:ss but if it comes differently then my code is breaking with ParseException.The incoming date format I cant guess as they are already formatted in the database using to_date.Please tell me solution.

Thanks in advance...
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly does the string you are trying to parse look like, and which date pattern are you using exactly? If they don't match, you get a ParseException.

Note that the date pattern is case-sensitive. Upper-case YYYY means something different than lower-case yyyy. Make sure you use the correct letters in the pattern. The API documentation of java.text.SimpleDateFormat explains exactly what the pattern should look like.

Ofcourse you need to know exactly what the format is of dates you are trying to parse. Java won't guess automatically for you.
[ September 28, 2007: Message edited by: Jesper Young ]
 
Naveen Koneti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,

Our application is a very big application and it is a old one.So what the existing implementation doing is they are formatting the dates in these probable formats

1)dd-MM-yyyy HH:mm:ss
2)yyyy-MM-dd HH:mm:ss
3)dd-MMM-yy HH:mm:ss

And which format occurs when and in which page I dont know that.

what my requirement is when I encounter any date (it may come as java.util.Date or String)in the jsp I have to convert it according to the user Time zone and display.As of now it is not feasible to find the exact format of the date which is coming as there are 2000+ jsps present in my application.Is there any way to know the format of the date at runtime,I mean a generalized logic to determine the format of the source date ? so that I can take it parse and show to the user in the same format.

Please advice....

Thanks in advance
 
Sheriff
Posts: 21550
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can try parsing the strings using one format at a time, and then fail if all formats fail. It's exactly what )]DateUtils.parseDate from the Jakarta Commons Lang Library does.

If you don't want to use that library you can do the same yourself:
 
Naveen Koneti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks rob .....
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!