'Is this the best practice for SimpleDateFormat in spring boot?
The SimpleDateFormat is not a thread-safe class.
Is this the best practice to write a Utility Service for SimpleDateFormat in spring boot?
@Service
public class DateConvertUtil {
private SimpleDateFormat getDateFormateInstance() {
return new SimpleDateFormat("yyyy/MM/dd");
}
public String parseDateToString(Date date) {
SimpleDateFormat sdf = getDateFormateInstance();
return sdf.format(date);
}
public Date parseStringToDate(String date) throws ParseException {
SimpleDateFormat sdf = getDateFormateInstance();
return sdf.parse(date);
}
}
Solution 1:[1]
Base on my opinion :
- Annotation like
@Servicehave the default scope is singleton. - You do not contain any mutable state in the @Service, the problem of the thread-safe belong to the mutable state, which can be accessed between multiple thread and will make the system confused.
- Bonus : You can use @Configuration and @Bean for create bean for this Utils class.
So I think it does not harm the system as well. Just try it and find the problem. Best practices require lots of practices
Solution 2:[2]
It looks like that you have a faulty structure in your project . Create a package Utill and put there all your classes or methods which you want to share in your project
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JustARandomWibuuuu |
| Solution 2 | john triantafillakis |
