0

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.

Jens
  • 67,715
  • 15
  • 98
  • 113
Paramesh Korrakuti
  • 1,997
  • 4
  • 27
  • 39

1 Answers1

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