parsing dates with variable spaces

前端 未结 3 955
你的背包
你的背包 2020-12-06 12:13

I am using Joda to parse dates and have a format where leading zeros are not used, e.g.:

 Mon Nov 20 14:40:36 2006
 Mon Nov  6 14:40:36 2006
<
相关标签:
3条回答
  • 2020-12-06 12:39

    I tried using a single 'd' as suggested above in logstash 1.1.1 but it still complained about a malformed date when a single digit day with an extra leading space was parsed. The following logstash rules did work.

    timestamp => [ "MMM dd HH:mm:ss", "MMM  d HH:mm:ss" ]
    

    It didn't matter which order the two date formats were in. No more warnings were output once I added both formats.

    0 讨论(0)
  • 2020-12-06 12:44

    I have just created a quick program to check this -

    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy");
    
    try {
        String source1 = "Mon Nov 20 14:40:36 2006";
        Date d1 = sdf.parse(source1);
        String source2 = "Mon Nov  6 14:40:36 2006";
        Date d2 = sdf.parse(source2);
    
        String res1 = sdf.format(d1);
        String res2 = sdf.format(d2);
    
        System.out.println(source1 +"="+ res1);
        System.out.println(source2 +"="+ res2);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    

    The output from this is -

    Mon Nov 20 14:40:36 2006=Mon Nov 20 14:40:36 2006
    Mon Nov  6 14:40:36 2006=Mon Nov 6 14:40:36 2006
    

    So, even though source2 has the extra space, it is still parsed by

    EEE MMM d HH:mm:ss yyyy
    

    Hope that helps

    0 讨论(0)
  • 2020-12-06 12:52

    java.time and format pattern letter p

    Here’s the modern answer, using java.time, the successor of Joda-Time.

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM ppd HH:mm:ss uuuu", Locale.ENGLISH);
    
        String[] stringsToParse = {
                "Mon Nov 20 14:40:36 2006",
                "Mon Nov  6 14:40:36 2006"
        };
        for (String dateTimeString : stringsToParse) {
            LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
            System.out.println(dateTime);
        }
    

    Output:

    2006-11-20T14:40:36
    2006-11-06T14:40:36
    

    To DateTimeFormatter.ofPattern format letter p means padding with spaces on the left. pp means padding to two position. It can be used for both formatting and — as here — parsing.

    I know you asked about Joda-Time. The Joda-Time home page says:

    Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

    Links

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Documentation of DateTimeFormatter
    • Joda-Time - Home
    0 讨论(0)
提交回复
热议问题