*
The moose likes Java in General and the fly likes Getting problem in SimpleDateFormate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting problem in SimpleDateFormate" Watch "Getting problem in SimpleDateFormate" New topic
Author

Getting problem in SimpleDateFormate

Joshi Nirav
Greenhorn

Joined: Dec 04, 2009
Posts: 14

Sometimes its creating problem for the parsing the date in formate yyyy-mm-dd and giving a ParseException even the date is passing in the yyyy-mm-dd format.
I have created the static instance of the SimpleDateFormat in my class and i am going to parse the date using the static method.

I am running this utility on the Web logic environment.

public static final String WS_DATE_PATTERN = "yyyy-MM-dd";
static SimpleDateFormat sdfWS = new SimpleDateFormat(WS_DATE_PATTERN);
.
.
.
public static Calendar convertWSDate(String date) throws ParseException{
Calendar calendar = Calendar.getInstance();
calendar.setTime(DateUtils.sdfWS.parse(date));
return calendar;
}

Please Guide me whats the issue as soon as possible.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Joshi Nirav wrote:Please Guide me whats the issue as soon as possible.

Please EaseUp.

Your code looks to be fine. Can you give us the strings that cause the ParseExceptions? Is there perhaps any trailing whitespace or something like that?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joshi Nirav
Greenhorn

Joined: Dec 04, 2009
Posts: 14

Hi
Thanks a lot for your reply.
But i had tried this code by providing the white space and all other posibility then too its working.

But as i said in my earlier message
the error seldom reproduces.

Will it create any issue while more than two threads trying to access the SimpleDateFormate class ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Joshi Nirav wrote:Will it create any issue while more than two threads trying to access the SimpleDateFormate class ?

Yes, that can certainly be the cause of the problem, because class SimpleDateFormat is not thread-safe. If you have two threads that use the same SimpleDateFormat at the same time, you can get strange and unpredictable errors. Note that the Javadoc API documentation of class SimpleDateFormat mentions this:
Synchronization

Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Definitely. SimpleDateFormat, or any other Format, is not thread safe unless explicitly mentioned. As far as I know, none of the Format classes in the core API is thread safe.

If you need a thread safe way of parsing dates, use a ThreadLocal object:
sdfWS.get() returns a new SimpleDateFormat object for each thread. Every call within one thread will return the same SimpleDateFormat.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting problem in SimpleDateFormate
 
Similar Threads
SimpledateFormat class issue.
xml date conversion all date conversions
Need to change Calendar to dd/mm/yyyy format
java.util.Date
Convert the String to date format