Java8 - Formatting and parsing date and time

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 07:36:51 Viewed : 350


In Java 8, the java.time.format.DateTimeFormatter class provides a flexible and powerful way to format and parse dates and times. DateTimeFormatter allows you to define patterns for formatting and parsing, making it suitable for a wide range of date and time representations.

Formatting Date and Time:

To format a LocalDate, LocalTime, LocalDateTime, or ZonedDateTime, you can use the DateTimeFormatter as follows:

java
import java.time.*; import java.time.format.DateTimeFormatter; public class DateTimeFormattingExample { public static void main(String[] args) { // Format LocalDate LocalDate date = LocalDate.now(); DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String formattedDate = date.format(dateFormatter); System.out.println("Formatted Date: " + formattedDate); // Format LocalTime LocalTime time = LocalTime.now(); DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); String formattedTime = time.format(timeFormatter); System.out.println("Formatted Time: " + formattedTime); // Format LocalDateTime LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = dateTime.format(dateTimeFormatter); System.out.println("Formatted DateTime: " + formattedDateTime); // Format ZonedDateTime ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York")); DateTimeFormatter zonedDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); String formattedZonedDateTime = zonedDateTime.format(zonedDateTimeFormatter); System.out.println("Formatted ZonedDateTime: " + formattedZonedDateTime); } }

In this example, different DateTimeFormatter instances are used to format LocalDate, LocalTime, LocalDateTime, and ZonedDateTime. The patterns used in the ofPattern method define how the date and time components are formatted.

Parsing Date and Time:

To parse a string into a LocalDate, LocalTime, LocalDateTime, or ZonedDateTime, you can use the same DateTimeFormatter:

java
import java.time.*; import java.time.format.DateTimeFormatter; public class DateTimeParsingExample { public static void main(String[] args) { // Parse LocalDate String dateStr = "2022-03-15"; DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate parsedDate = LocalDate.parse(dateStr, dateFormatter); System.out.println("Parsed Date: " + parsedDate); // Parse LocalTime String timeStr = "14:30:00"; DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); LocalTime parsedTime = LocalTime.parse(timeStr, timeFormatter); System.out.println("Parsed Time: " + parsedTime); // Parse LocalDateTime String dateTimeStr = "2022-03-15 14:30:00"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeStr, dateTimeFormatter); System.out.println("Parsed DateTime: " + parsedDateTime); // Parse ZonedDateTime String zonedDateTimeStr = "2022-03-15 14:30:00 America/New_York"; DateTimeFormatter zonedDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); ZonedDateTime parsedZonedDateTime = ZonedDateTime.parse(zonedDateTimeStr, zonedDateTimeFormatter); System.out.println("Parsed ZonedDateTime: " + parsedZonedDateTime); } }

In this example, DateTimeFormatter is used to parse strings into LocalDate, LocalTime, LocalDateTime, and ZonedDateTime. The patterns used in the ofPattern method must match the format of the input string.

Common Patterns:

Here are some common patterns used in formatting and parsing:

  • y: Year (4 digits)
  • M: Month (1-2 digits)
  • d: Day of month (1-2 digits)
  • H: Hour (0-23)
  • m: Minute (0-59)
  • s: Second (0-59)
  • z: Time zone abbreviation (e.g., "UTC", "America/New_York")

You can customize patterns according to your specific requirements. The full list of pattern letters and their meanings can be found in the official documentation.

Search
Related Articles

Leave a Comment: