java SimpleDateFormat

后端 未结 4 1542
甜味超标
甜味超标 2020-12-15 23:51

in Java, how to parse a date string that contains a letter that does not represent a pattern?

\"2007-11-02T14:46:03+01:00\"
String date =\"2007-11-0         


        
相关标签:
4条回答
  • 2020-12-16 00:19

    You can try

    String format = "yyyy-MM-dd'T'HH:mm:ssz";
    

    Reference : from Javadoc

    Text can be quoted using single quotes (') to avoid interpretation.

    0 讨论(0)
  • 2020-12-16 00:27

    If you don't care about the time zone, you can use this method.

      public static Date convertToDate(String strDate) throws ParseException {
        Date date = null;
        if (strDate != null) {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
          date = sdf.parse(strDate);
        }
        return date;
      }
    

    I don't know if it's still useful for you, but I encounter with the same problem now, and after a little I come up with this.

    0 讨论(0)
  • 2020-12-16 00:32
    String testDate = "2007-11-02T14:46:03+01:00";
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
    Date date = formatter.parse(testDate);
    System.out.println(date);
    

    You can try similar to the above

    You can use following link for reference

    0 讨论(0)
  • 2020-12-16 00:40

    The time you're trying to parse appears to be in ISO 8601 format. SimpleDateFormat unfortunately doesn't support all the same timezone specifiers as ISO 8601. If you want to be able to properly handle all the forms specified in the ISO, the best thing to do is use Joda time.

    This example is straight out of the user guide:

    DateTime dt = new DateTime("2004-12-13T21:39:45.618-08:00");
    
    0 讨论(0)
提交回复
热议问题