Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Date and Time Literals

Header Image

Ahoy there, matey! Welcome to our next adventure in the world of programming. In our previous journey, we explored the basics of the Java DateTime library. Today, we will set our sails and explore one of the crucial aspects of working with dates and times - Date and Time Literals.

String Format of Date and Time

Dates and times are usually represented as a string in a specific format. This format helps to understand and identify the elements present in the date and time. The DateTime library provides various predefined patterns to format dates and times. Let’s set our bearings and explore some of the essential patterns.

  • yyyy-MM-dd - This pattern represents the year (yyyy), followed by the month (MM), and then the day (dd). For example, the string “2022-05-15” represents May 15th, 2022.

  • HH:mm:ss - This pattern represents the hour (HH), followed by the minute (mm), and then the seconds (ss). For example, the string “16:30:00” represents 4:30 PM.

  • yyyy-MM-dd HH:mm:ss - This pattern combines the above two patterns and represents the complete date and time. For example, the string “2022-05-15 16:30:00” represents May 15th, 2022, at 4:30 PM.

These are just a few examples of the many patterns available in the DateTime library. Each pattern has its own set of characters and symbols, which represent the various elements of the date and time. It is essential to choose the correct pattern, depending on the requirements of the program.

Conversion to Date and Time Types

Now that we have set our bearings and explored the different patterns, let’s see how we can convert these strings to date and time types. The DateTime library provides the parse() method, which converts the string to the corresponding date or time type.

//Converting string to LocalDate type
String dateStr = "2022-05-15";
LocalDate date = LocalDate.parse(dateStr);

//Converting string to LocalTime type
String timeStr = "16:30:00";
LocalTime time = LocalTime.parse(timeStr);

//Converting string to LocalDateTime type
String dateTimeStr = "2022-05-15 16:30:00";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr);

The parse() method is overloaded and can take additional parameters, such as the pattern used to represent the date and time. If the pattern is not provided, the method uses the default pattern for that specific type.

In conclusion, understanding date and time literals and their string format is crucial in working with the DateTime library. It helps to identify the elements present in the date and time and represents them in a specific format. Additionally, the DateTime library provides various predefined patterns and methods to convert the string to the corresponding date and time type. Hoist the sail, me hearties! Our adventure with Java DateTime library continues!

Conversion to Date and Time Types (Continued)

Sometimes, we may need to use a custom pattern to parse the string to a date or time type. In such cases, we can use the DateTimeFormatter class. This class provides methods to create custom patterns and use them to parse the string.

//Custom pattern for parsing string to LocalDate type
String dateStr = "15-05-2022";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(dateStr, dateFormatter);

//Custom pattern for parsing string to LocalTime type
String timeStr = "4:30 PM";
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a");
LocalTime time = LocalTime.parse(timeStr, timeFormatter);

In the above examples, we have created custom patterns using the DateTimeFormatter class and used them to parse the string to the corresponding date or time type.

It is important to note that the format of the string and the pattern provided should match. Otherwise, it may result in a DateTimeParseException.

Conclusion

Ahoy there! We have reached the end of our adventure with Date and Time Literals in the Java DateTime library. In this journey, we explored the string format of date and time literals and the various patterns available in the DateTime library. We also learned about the parse() method and the DateTimeFormatter class, which can be used to convert the string to the corresponding date or time type.

Remember, working with dates and times can be challenging, but with the DateTime library, it becomes a breeze. Keep exploring, me hearties! May the winds of programming take you to new and exciting adventures.