Time Formatting Parsing in CLIPS

Java supports time formatting and parsing via pattern-based layouts, similar to other languages. We’ll use the java.time package for this functionality.

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);

        // We 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.format(customFormatter));

        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 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());

        // 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());
        }
    }
}

To run this program, save it as TimeFormattingParsing.java and use javac to compile it, then java to run it:

$ javac TimeFormattingParsing.java
$ java TimeFormattingParsing
2023-05-15T10:30:15.123456
2012-11-01T22:08:41Z
10:30AM
Mon May 15 10:30:15 2023
2023-05-15T10:30:15.123456-07:00
2023-05-15T20:41
2023-05-15T10:30:15-00:00
Parsing error: Text '8:41PM' could not be parsed at index 0

Java’s java.time package provides a rich set of classes for handling dates and times. The DateTimeFormatter class is used for parsing and formatting date-time objects. Unlike Go, Java uses pattern strings directly instead of example-based layouts.

The LocalDateTime class represents a date-time without a time zone, while ZonedDateTime includes time zone information. For parsing times without dates, you might need to use LocalTime instead.

Java’s date-time API is more type-safe compared to Go’s, with different classes for different use cases (e.g., LocalDate for date-only, LocalTime for time-only). This can make it more verbose but also helps prevent certain types of errors.

Remember to handle DateTimeParseException when parsing potentially invalid input, as shown in the last example.

查看推荐产品

Comments powered by Disqus