aspose file tools
The moose likes Java in General and the fly likes Regarding date conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Regarding date conversion" Watch "Regarding date conversion" New topic
Author

Regarding date conversion

Naveen Koneti
Greenhorn

Joined: Apr 27, 2007
Posts: 13
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...
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12953
    
    3

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Naveen Koneti
Greenhorn

Joined: Apr 27, 2007
Posts: 13
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

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:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Naveen Koneti
Greenhorn

Joined: Apr 27, 2007
Posts: 13
Thanks rob .....
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Regarding date conversion
 
Similar Threads
Example of convert String to Date
date time conversion
How do I pad a timestamp string with 0s?
Time format in Java
how to change the date,Time format after accessing it from the database