Time Formatting Parsing in Karel

Java supports time formatting and parsing via pattern-based layouts, similar to other languages. The java.time package provides classes for handling date and time operations.

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
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_DATE_TIME, which is similar to RFC3339
        ZonedDateTime now = ZonedDateTime.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 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.SSSSSSVV");
        System.out.println(now.format(customFormatter));

        String timeString = "8 41 PM";
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("h mm a");
        LocalDateTime t2 = LocalDateTime.parse(timeString, parser);
        System.out.println(t2);

        // For purely numeric representations you can also
        // use standard string formatting 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());

        // DateTimeParseException will be thrown on malformed input
        try {
            LocalDateTime.parse("8:41PM", DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy"));
        } catch (Exception e) {
            System.out.println(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 ZonedDateTime.now() to get the current date and time with timezone information.

  2. We format this time using the ISO_DATE_TIME formatter, which is similar to RFC3339.

  3. We parse a string representation of a date and time using the same formatter.

  4. We create custom formatters using DateTimeFormatter.ofPattern() to format the time in different ways.

  5. We parse a time string using a custom formatter.

  6. We use String.format() to create a custom numeric representation of the date and time.

  7. Finally, we demonstrate error handling when parsing fails.

To run this program, save it as TimeFormattingParsing.java, compile it with javac TimeFormattingParsing.java, and run it with java TimeFormattingParsing.

The output will vary based on the current time when you run it, but it will demonstrate various formatted and parsed date-time representations.