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.
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?
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.
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.