How to get the given date string format(pattern) in java?

后端 未结 10 1635
抹茶落季
抹茶落季 2020-11-30 04:41

I want to get the format of a given date string.

Example: I have a string like 2011-09-27T07:04:21.97-05:00 and the date format of this string is

10条回答
  •  有刺的猬
    2020-11-30 05:02

    You could try dateparser.

    It can recognize any String automatically, and parse it into Date, Calendar, LocalDateTime, OffsetDateTime correctly and quickly(1us~1.5us).

    It doesn't based on any natural language analyzer or SimpleDateFormat or regex.Pattern.

    With it, you don't have to prepare any appropriate patterns like yyyy-MM-dd'T'HH:mm:ss'Z' or MM/dd/yyyy HH:mm:ss etc:

    Date date = DateParserUtils.parseDate("2015-04-29T10:15:00.500+0000");
    Calendar calendar = DateParserUtils.parseCalendar("2015-04-29T10:15:00.500Z");
    LocalDateTime dateTime = DateParserUtils.parseDateTime("2015-04-29 10:15:00.500 +00:00");
    

    And it has better performance than loop-try multiple SimpleDateFormat.

    Please enjoy it.

提交回复
热议问题