Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

java - how to parse 'following string '20190911T14:37:08.7770400' into date format

I am trying to parse the String into simple date format but getting unable to parse date exception.could some one try to help me out on this.

  String dateFormatter ="20190911T14:37:08.7770400";
  SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDD'T'HH:mm:ss.SSSZZZZ");
  //tried the below commnet also
  //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-d'T'HH:mm:ss.SSSZZZZ");
  System.out.println(sdf.parse(dateFormatter));


 Error: 
       "Exception in thread "main" java.text.ParseException: Unparseable 
       date: "20190911T14:37:08.7770400" at 
       java.text.DateFormat.parse(DateFormat.java:366) at 
      com.sudha.test.sample_boot_example.TestString.main(TestString.java:20)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The timezone portion of your format is expecting a leading + or - character. As such the string you gave it fails. If you try it with "20190911T14:37:08.777+0400" it works.

I don't see any options that will work with a 4-digit timezone offset that doesn't have a sign. https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

Edit: Also as Andeas has pointed out in a comment, the year and day should be using lowercase, ‘yyyy’ and ‘dd’


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...