Time Formatting Parsing in Mercury

Our first example demonstrates time formatting and parsing in Java using the java.time package, which provides a modern date-time API.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
import java.time.ZoneOffset;

public class TimeFormattingParsing {
    public static void main(String[] args) {
        // Here's a basic example of formatting a time
        // according to ISO 8601 (similar to RFC3339)
        LocalDateTime now = LocalDateTime.now();
        String formatted = now.format(DateTimeFormatter.ISO_DATE_TIME);
        System.out.println(formatted);

        // Time parsing uses the same formatter as formatting
        ZonedDateTime parsed = ZonedDateTime.parse("2012-11-01T22:08:41+00:00", DateTimeFormatter.ISO_DATE_TIME);
        System.out.println(parsed);

        // You can create custom formatters using patterns
        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.SSSSSSXXX");
        System.out.println(now.atZone(ZoneOffset.systemDefault()).format(customFormatter));

        // Parsing with custom formatters
        customFormatter = DateTimeFormatter.ofPattern("h mm a");
        LocalDateTime customParsed = LocalDateTime.parse("8 41 PM", customFormatter);
        System.out.println(customParsed);

        // For purely numeric representations, you can use String.format
        System.out.printf("%d-%02d-%02dT%02d:%02d:%02d-00:00%n",
            now.getYear(), now.getMonthValue(), now.getDayOfMonth(),
            now.getHour(), now.getMinute(), now.getSecond());

        // Parsing will throw an exception on malformed input
        try {
            customFormatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy");
            LocalDateTime.parse("8:41PM", customFormatter);
        } catch (Exception e) {
            System.out.println("Parsing error: " + e.getMessage());
        }
    }
}

This Java code demonstrates various ways to format and parse dates and times. Here’s a breakdown of what’s happening:

  1. We use LocalDateTime.now() to get the current date and time.

  2. We format the current time using the ISO date-time formatter, which is similar to RFC3339.

  3. We parse a specific date-time string using the ISO date-time formatter.

  4. We create custom formatters using DateTimeFormatter.ofPattern() and use them to format the current time in various ways.

  5. We demonstrate parsing a time string using a custom formatter.

  6. We show how to format date-time components individually using String.format().

  7. Finally, we show how parsing throws an exception when given malformed input.

Java’s java.time package provides a rich set of classes and methods for handling dates, times, and durations. The DateTimeFormatter class is particularly useful for creating custom date-time formats.

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 show various formatted date-times and parsing results.