Java ParseException while attempting String to Date parsing

前端 未结 5 1047
生来不讨喜
生来不讨喜 2021-01-20 08:32

I\'m having a hard time Parsing/Formatting a Date string received back from a web service. I\'ve attempted multiple approaches, but with no luck.

Sample Dat

5条回答
  •  执笔经年
    2021-01-20 09:06

    You could try:

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    String dateString = dateString.replace("Z", "GMT+00:00");
    Date date = dateFormat.parse(dateString);
    

    The above code should correctly handle the case where a timezone is specified in the date. As Z represents the UTC/GMT timezone it is replaced by GMT so the SimpleDateFormat can interpret it correctly (i would love to know a cleaner way of handling this bit if anyone knows one).

提交回复
热议问题