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;
}
}