Currently I have a requirement to convert String to java.util.Date.
Found many tutorials to convert string to Date by using java.text.SimpleDateFormatter. But my concern in this SimpleDateFormatter is not thread safe.
Is there any possibility to convert string to java.util.Date by using java.time package in Java 8. Since java.time package classes are thread safe.
1 Answer
To answer the question about java.time from Java SE 8:
You could use:
LocalDateTime date = LocalDateTime.parse(someDateString);
and to convert it to date you could use:
Instant instant = date.atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);
2 Comments
Paramesh Korrakuti
Here
parse() method return type is LocaleDate, but I need java.util.DateBen Win
@ParameshKorrakuti added conversion
SimpleDateFormatis not thread safe is not a problem. Just create a new instance each time you need it. Premature optimization is the root of all evil.