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.

4
  • 5
    Java 8 and Android in the same question? Commented Mar 18, 2015 at 10:34
  • 1
    The 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. Commented Mar 18, 2015 at 10:36
  • 1
    I am not sure, whats wrong in this question to vote -1? Commented Mar 18, 2015 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 Commented Mar 18, 2015 at 11:01

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Here parse() method return type is LocaleDate, but I need java.util.Date
@ParameshKorrakuti added conversion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.