Java Unparsable date

后端 未结 4 1502
Happy的楠姐
Happy的楠姐 2020-12-21 16:37

I have a string with the format: String dateString = \"2014-03-17T20:05:49.2300963Z\"

Trying this:

SimpleDateFormat df = new SimpleDateF         


        
相关标签:
4条回答
  • 2020-12-21 16:44

    How to truncate the fractional seconds to milliseconds (because Java can't handle fractional seconds):

    public String truncate(String dateString){
      int numberOfDigits = dateString.substring(dateString.indexOf("."), dateString.length() - 1).length();
    
      String justMilliSecondsDate = null;
      if (numberOfDigits == 3) {
        justMicrosDate = dateString;
        } 
      else if (numberOfDigits > 3) {
        justMilliSecondsDate = dateString.substring(0, dateString.length() - numberOfDigits + 3);
      } 
      else {
          justMilliSecondsDate = dateString;
          for (int i = numberOfDigits ; i < 3 ; i++) justMilliSecondsDate += "0";
      }
    
      return justMilliSecondsDate;
    }
    
    0 讨论(0)
  • 2020-12-21 16:57

    You need to provide as many S as you have in your date String. In this case, 7

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.SSSSSSSX");
    

    This is required because, otherwise, the DateFormat doesn't know where the milliseconds end and where the time zone starts.


    Note also, that

    2300963
    

    as a millisecond value means 2300 seconds and 963 milliseconds. Why do you have it that way? Why aren't those seconds part of the value in their corresponding position? When the DateFormat parses it, they will be added.

    0 讨论(0)
  • 2020-12-21 16:57

    tl;dr

    Instant.parse( "2014-03-17T20:05:49.2300963Z" )
    

    java.time

    You are using the troublesome old date-time classes, now legacy, supplanted by the java.time classes.

    Resolution

    • Milliseconds
      The old date-time classes were limited to milliseconds resolution, for up to three digits of decimal fraction.
    • Microseconds
      Your input has six digits of fractional second for microseconds.
    • Nanoseconds
      The java.time classes have a resolution of nanoseconds for up to nine digits of fractional seconds. More than enough for your microseconds.

    ISO 8601

    The ISO 8601 standard defines string formats to represent date-time values. You input complies with ISO 8601.

    The java.time classes use ISO 8601 formats by default when parsing and generating strings. So no need to specify a formatting pattern.

    Instant instant =
        Instant.parse( "2014-03-17T20:05:49.2300963Z" );
    
    0 讨论(0)
  • 2020-12-21 17:03

    This works: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.SSSSSSS'Z'");

    1. It's safer to specify exactly how much precision you expect (say, for milliseconds in this case). It's odd to have 7 digits but if all your dates look like this, put 7 S.
    2. The X will parse a timezone of the sort -0800. So your string would have to look like 2014-03-17T20:05:49.2300963-0800 (or something similar). Treat the Z as a literal, like T.

    EDIT: Relevant to your partial seconds issue.

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