• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Getting problem in SimpleDateFormate

 
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.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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 ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic