Understanding JodaTime DateTime.parse(string, formatter)

前端 未结 2 1805
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 03:50

Does the style of the formatter in the parse method of the DateTime class have to match the exact style of the string? For instance, I\'m getting a TimeStamp object from the

相关标签:
2条回答
  • 2020-12-31 03:56

    Use LocalDateTime instead:

    String input = "08-AUG-12 12.00.00 AM";
    String pattern = "dd-MMM-yy hh.mm.ss aa";
    
    LocalDateTime localDateTime = LocalDateTime.parse(input, DateTimeFormat.forPattern(pattern));
    

    EDIT

    As a matter of fact you can do it with DateTime also:

    private static String parseDateTime(String input){
         String pattern = "dd-MMM-yy hh.mm.ss aa";
         DateTime dateTime  = DateTime.parse(input, DateTimeFormat.forPattern(pattern));
         return dateTime.toString("dd-MMM-yy hh:mm:ss aa");
    }
    
    0 讨论(0)
  • 2020-12-31 04:23

    Figured it out. To get the correct format, you have to call formatter.print(localDateTime object) and it worked.

    0 讨论(0)
提交回复
热议问题