Time Formatting Parsing in Miranda

Java supports time formatting and parsing via the SimpleDateFormat class and the java.time package introduced in Java 8.

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

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)
        Instant now = Instant.now();
        System.out.println(DateTimeFormatter.ISO_INSTANT.format(now));

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

        // SimpleDateFormat and DateTimeFormatter use pattern-based layouts
        // You can supply custom layouts using these classes
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mma");
        System.out.println(formatter.format(localDateTime));

        formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy");
        System.out.println(formatter.format(localDateTime));

        formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
        System.out.println(formatter.format(localDateTime.atZone(ZoneId.systemDefault())));

        SimpleDateFormat sdf = new SimpleDateFormat("h mm a");
        try {
            Date date = sdf.parse("8 41 PM");
            System.out.println(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 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",
                localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth(),
                localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());

        // SimpleDateFormat will throw a ParseException on malformed input
        sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
        try {
            sdf.parse("8:41PM");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

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 be similar to:

2023-05-15T12:34:56.789Z
2012-11-01T22:08:41Z
12:34PM
Mon May 15 12:34:56 2023
2023-05-15T12:34:56.789000-07:00
Thu Jan 01 20:41:00 PST 1970
2023-05-15T12:34:56-00:00
java.text.ParseException: Unparseable date: "8:41PM"

Note that Java’s date and time API has evolved over time. The example above uses both the older SimpleDateFormat class and the newer java.time package introduced in Java 8. The java.time package is generally preferred for new code due to its improved design and thread-safety.

In Java, date-time formatting patterns are different from those in Go. For example, “yyyy” is used for a 4-digit year, “MM” for month, “dd” for day, “HH” for hour in 24-hour format, “mm” for minutes, and “ss” for seconds. The full list of pattern letters can be found in the Java documentation for SimpleDateFormat and DateTimeFormatter.

Java’s date-time API is quite flexible and powerful, allowing for various operations and conversions between different date-time representations.