Time Formatting Parsing in Java
Our first program will demonstrate time formatting and parsing in Java. Here’s the full source code:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class TimeFormattingParsing {
public static void main(String[] args) {
// Here's a basic example of formatting a time
// according to ISO_DATE_TIME, which is similar to RFC3339
LocalDateTime now = LocalDateTime.now();
System.out.println(now.format(DateTimeFormatter.ISO_DATE_TIME));
// Time parsing uses the same formatter as formatting
ZonedDateTime t1 = ZonedDateTime.parse("2012-11-01T22:08:41+00:00", DateTimeFormatter.ISO_DATE_TIME);
System.out.println(t1);
// You can also create custom formatters
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("h:mma");
System.out.println(now.format(customFormatter));
customFormatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy");
System.out.println(now.format(customFormatter));
customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSX");
System.out.println(ZonedDateTime.now().format(customFormatter));
// Parsing with custom formatters
String timeString = "8:41 PM";
customFormatter = DateTimeFormatter.ofPattern("h:mm a");
LocalDateTime t2 = LocalDateTime.parse(timeString, customFormatter);
System.out.println(t2);
// For purely numeric representations you can also use String.format
// with the extracted components of the time value
System.out.printf("%d-%02d-%02dT%02d:%02d:%02d-00:00%n",
now.getYear(), now.getMonthValue(), now.getDayOfMonth(),
now.getHour(), now.getMinute(), now.getSecond());
// Parse will throw a DateTimeParseException on malformed input
try {
customFormatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy");
LocalDateTime.parse("8:41PM", customFormatter);
} catch (DateTimeParseException e) {
System.out.println("Parsing error: " + e.getMessage());
}
}
}
This program demonstrates various ways to format and parse dates and times in Java using the java.time
package, which was introduced in Java 8.
Here’s a breakdown of what the code does:
We start by formatting the current time using the ISO_DATE_TIME formatter, which is similar to RFC3339.
We then parse a specific date-time string using the same formatter.
We demonstrate how to create custom formatters using pattern strings. These patterns use specific letters to represent different parts of a date or time.
We show how to parse a time string using a custom formatter.
We use
String.format
to create a purely numeric representation of the current time.Finally, we show how parsing can throw a
DateTimeParseException
when given malformed input.
To run this program, save it as TimeFormattingParsing.java
, compile it with javac TimeFormattingParsing.java
, and then run it with java TimeFormattingParsing
.
The output will vary depending on the current time when you run it, but it will look something like this:
2023-06-15T10:30:15.123456
2012-11-01T22:08:41Z
10:30AM
Thu Jun 15 10:30:15 2023
2023-06-15T10:30:15.123456-07:00
0000-01-01T20:41
2023-06-15T10:30:15-00:00
Parsing error: Text '8:41PM' could not be parsed at index 0
This example showcases Java’s powerful date and time manipulation capabilities, which are more type-safe and less error-prone than the older java.util.Date
and java.util.Calendar
classes.