Skip to content

SIMPLE_DATE_FORMAT have a different behavior for X letter compared to Java #9822

Open
@snleee

Description

Java's SimpleDateFormat (https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) has a X letter to represent the time zone in the format of -08:00. The behavior of Pinot's SIMPLE_DATE_FORMAT in DateTimeFieldSpec is actually different from Java.

input = 2022-11-16 15:22:37.123+00:00
SIMPLE_DATE_FORMAT|yyyy-MM-dd HH:mm:ss.SSSX <- this is the config based on Java doc but it's not working
SIMPLE_DATE_FORMAT|yyyy-MM-dd HH:mm:ss.SSSZ <- this is the working config but it's not supposed to work based on Java definition.

Here is the code to reproduce:

  public static void main(String[] args)
      throws ParseException {
    String input = "2022-11-16 15:22:37.123+00:00";

    // This is working and this is the expected behavior based on the Java documentation.
    String pattern = "yyyy-MM-dd HH:mm:ss.SSSX";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    Date date = simpleDateFormat.parse(input);
    System.out.println(date.toInstant().toEpochMilli());

    // This is not working. But, it is supposed to work if we follow the convention for SimpleDateFormat
    try {
        String format = "SIMPLE_DATE_FORMAT|yyyy-MM-dd HH:mm:ss.SSSX";
      DateTimeFormatSpec dateTimeFormatSpec = new DateTimeFormatSpec(format);
      long millis = dateTimeFormatSpec.getDateTimeFormatter().parseMillis(input);
      System.out.println(millis);
    } catch (Exception e) {
      // not working
      System.out.println("failed");
    }

    // This is working and this is the expected behavior based on the documentation from JAVA.
    try {
      String pattern2 = "yyyy-MM-dd HH:mm:ss.SSSZ";
      simpleDateFormat = new SimpleDateFormat(pattern2);
      date = simpleDateFormat.parse(input);
      System.out.println(date.toInstant().toEpochMilli());
    } catch (Exception e) {
      // not working
      System.out.println("failed");
    }

    // This working but Java's SimpleDateFormat's definition for `Z` doesn't include "-00:00" so it supposed to fail.
    String format2 = "SIMPLE_DATE_FORMAT|yyyy-MM-dd HH:mm:ss.SSSZ";
    DateTimeFormatSpec dateTimeFormatSpec = new DateTimeFormatSpec(format2);
    long millis2 = dateTimeFormatSpec.getDateTimeFormatter().parseMillis(input);
    System.out.println(millis2);
  }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions