Optional parts in SimpleDateFormat

后端 未结 7 1589
挽巷
挽巷 2020-12-09 01:09

I\'m reading in date strings that could be with or without a time zone adjustment: yyyyMMddHHmmssz or yyyyMMddHHmmss. When a string is missing a z

相关标签:
7条回答
  • 2020-12-09 01:50

    If you can use Joda Datetime it supports optional parts in formatter, for example, "yyyy-MM-dd [hh:mm:ss]"

    private DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    .append(DateTimeFormat.forPattern("yyyy-MM-dd"))                                            
    .appendOptional(
        new DateTimeFormatterBuilder()
        .appendLiteral(' ')
        .append(DateTimeFormat.forPattern("HH:mm:ss"))
        .toParser()
    .toFormatter();
    
    0 讨论(0)
提交回复
热议问题