Skip to main content

Posts

Showing posts with the label Convert a String to Date in Java

How to Convert a String to Date in Java

How to Convert a String to Date in Java Converting a string to a date in Java (or any programming language) is a fundamental skill and is useful to know when working on projects. Sometimes, it is easier to work with a string to represent a date, and then convert it to a date object for further use. Date   API The Date / Time API in Java works by default with the ISO 8601 format, which is (yyyy-MM-dd).By default all dates follow this format, and all strings that are converted should follow this if they are using the default formatter. parse() The date-time API provides parse () methods to parse a string that contains date information. To convert string objects to LocalDateTime objects, the string must represent a valid date or time according to ISO_LOCAL_DATE Example, let's convert a String to a java.time . LocalDate : LocalDate date = LocalDate.parse("2018-05-05"); Parse API with a Custom Formate This format specifies three character...