Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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
Sheriff
Posts: 16081
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: 21255
87
Chrome Eclipse IDE Java 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 .....
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!