Using Alphabetic Characters in SimpleDateFormat Pattern String

前端 未结 1 351
走了就别回头了
走了就别回头了 2021-01-31 13:06

Is it possible to include an alphabetic character in a SimpleDateFormat Pattern String?

I am trying to create a format string where the letter \'T\' is included before t

相关标签:
1条回答
  • 2021-01-31 13:59

    Surrounding the T with single quotes should work:

    yyyy-MM-dd'T'hh:mm:ssZ
    

    Quoting from the documentation (emphasis mine):

    Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

    Your specific use case is even included as an example:

    Date and Time Pattern            Result
    -------------------------------------------------------------
    "yyyy-MM-dd'T'HH:mm:ss.SSSZ"     2001-07-04T12:08:56.235-0700  
    
    0 讨论(0)
提交回复
热议问题