• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Format method in SimlpleDateFormat

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Everybody knows that the SimpleDateFormat is not thread safe.

If the format of the date same for all the thread then format method in the SimpleDateFormat class is thread safe or not?

If the format of the data is same for all the thead then can we use a singel SimpleDateFormat instance for all the thread?

Does the below sample is thread safe (When 'dd/MM/yyyy' and 'MM/dd/yyyy' formats used) of not?
public class Helper {

private Static final SimpleDateFormat formatDDMMYY = new SimpleDateFormat("dd/MM/yyyy");

private Static final SimpleDateFormat formatMMDDYYYY = new SimpleDateFormat("MM/dd/yyyy");


public String formatDate(String format, Date date) {
SimpleDateFormat format = getFormater(format);
return format.format(date);
}

private SimpleDateFormat getFormater(String format) {
SimpleDateFormat format = null;
if(format.equals("dd/MM/yyyy")) {
format = formatDDMMYY ;
} else if (format.equals("MM/dd/yyyy")) ) {
format = formatMMDDYYYY ;
} else {
format = new SimpleDateFormat(format);
}
return format;
}



}

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's thread safe in this way. If an object can't change(theoretically immutable) then it is safe to use it from different threads. You can make getFormatter and formatDate static, since they don't use any field from the object.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SimpleDateFormat is indeed not thread-safe - this means you must not use the same SimpleDateFormat in different threads at the same time. It doesn't matter if you don't change the format; the SimpleDateFormat object seems to have some internal state that is modified when you call format().

Your code is not thread-safe.

Wrap your SimpleDateFormat object in a ThreadLocal:


p.s. Please use code tags when you post source code.
 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops, you are right Jesper. Sorry for my missleading comment.
 
velmurugan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic