To parse an ISO-8601 string into an instance of LocalDateTime, you can use the parse() method as shown below: // ISO-8601 string String str = "T11:25" // parse string to date and time LocalDateTime dateTime = LocalDateTime. It stores a combination of date and time without timezone in ISO-8601 format (yyyy-MM-ddTHH:mm). The LocalDateTime is the most popular class for handling date and time together in Java 8 and higher. parse (customStr, formatter ) Convert a string to LocalDateTime object ofPattern ( "hh:mm a" ) // parse string to time LocalTime customTime = LocalTime. of ( 8, 45 ) įor none ISO-8601 string formats, you have to pass a formatter using DateTimeFormatter as shown below: // custom string format String customStr = "10:15 PM" // define formatter DateTimeFormatter formatter = DateTimeFormatter. The above code is equivalent to writing the following code to instantiate an instance of LocalTime: LocalTime time = LocalTime. Just like LocalDate, you can use the LocalTime.parse() method to convert a string to a LocalTime object, as shown below: // ISO-8601 string String str = "08:45" // parse string to time LocalTime time = LocalTime. BASIC_ISO_DATE ) Convert a string to LocalTime objectĪ LocalTime instance represents a time without the date or timezone information in ISO-8601 format. Here is another example that uses the pre-defined formatter BASIC_ISO_DATE from DateTimeFormatter to parse a string into a LocalDate: // string in basic ISO-8601 format String str = "20191222" // parse string to date LocalDate date = LocalDate. ofPattern ( "MMMM dd, yyyy" ) // parse string to date LocalDate customDate = LocalDate. If the string is not in ISO-8601 format, you must define a custom formatter using DateTimeFormatter as shown below: // custom string format String customStr = "December 22, 2019" // define formatter DateTimeFormatter formatter = DateTimeFormatter. The above code is equivalent to writing the following code to instantiate a LocalDate instance: LocalDate date = LocalDate. To parse an ISO-8601 string to an instance of LocalDate, you can do the following: // ISO-8601 string String str = "" // parse string to date LocalDate date = LocalDate. Unlike Date, LocalDate provides utility methods to parse and format dates and add or subtract different units like days, months, and years. It differs from the old Date and doesn't store time or timezone information. The LocalDate class represents a date in ISO-8601 format (yyyy-MM-dd) without any time information. Otherwise, a DateTimeParseException will be thrown at runtime.Īlternatively, you can pass another parameter to parse() to explicitly define the string pattern: parse ( CharSequence text, DateTimeFormatter formatter )Ī DateTimeFormatter instance is a formatter for formatting and parsing date-time objects in Java 8 new date and time API. The input string must be in ISO-8601 format to convert a string into an instance of date using the above method. The new API provides the parse() method that accepts a sequence of characters as an argument and uses the ISO_LOCAL_DATE format to parse the string into a date: parse ( CharSequence text ) These classes use ISO-8601 format to represent dates and times. Java 8 introduced a new date and time API (classes in the java.time.* package) to make it easy to work with dates in Java. Finally, we'll discuss 3rd-party libraries like Apache Commons Lang that can also be used to perform this conversion. We'll first look at utility methods provided by Java 8 new date and time API to perform the string-to-date conversion.Īfterward, we'll look at the legacy class used to represent dates. In this tutorial, you'll learn different ways to convert a string object to a date object in Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |