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.
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 ]
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
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.
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: