This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting problem in SimpleDateFormate

 
Joshi Nirav
Greenhorn
Posts: 14
IntelliJ IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joshi Nirav
Greenhorn
Posts: 14
IntelliJ IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic