How can I convert a String object to a Date object?
I think I need to do something like this:
Date d=(some conversion ) "String "
Any help would be greatly appreciated.
Using the Parse API With a Custom Formatter. Converting a String with a custom date format into a Date object is a widespread operation in Java. For this purpose we'll use the DateTimeFormatter class, which provides numerous predefined formatters, and allows us to define a formatter.
If the String value that we need to convert to the Date-time type is of ISO-801 format then we can simply call DateFormat and SimpleDateFormat classes using parse() methods.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date date = dateFormat.parse("1.1.2001");
For details refer to: SimpleDateFormat documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With