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.
Asked
Active
Viewed 2,419 times
0

Jens
- 67,715
- 15
- 98
- 113

Paramesh Korrakuti
- 1,997
- 4
- 27
- 39
-
5Java 8 and Android in the same question? – fge Mar 18 '15 at 10:34
-
1The fact that `SimpleDateFormat` is 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.* – Arnaud Denoyelle Mar 18 '15 at 10:36
-
1I am not sure, whats wrong in this question to vote -1? – Paramesh Korrakuti Mar 18 '15 at 10:41
-
Are you definitely using it in a multi-threaded environment? If not, you don't need to worry about it being threadsafe – Trisha Mar 18 '15 at 11:01
1 Answers
3
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);

Ben Win
- 830
- 8
- 21
-
Here `parse()` method return type is `LocaleDate`, but I need `java.util.Date` – Paramesh Korrakuti Mar 18 '15 at 10:45
-