Java SimpleDateFormat: Pattern - ParseException [duplicate]

爷,独闯天下 提交于 2019-12-20 05:39:24

问题


I am struggling with a date string, I need to parse into the java ‘Date’ object. Here is what I have got so far:

try {

String value =   "2017‎-‎11‎-‎23T14:00:49.184000000Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'";

SimpleDateFormat parser = new SimpleDateFormat(pattern);

Date date = parser.parse(value);


} catch (ParseException e) {e.printStackTrace();}

It currently throws a ParseExceptionUnparseable date” and I can’t get it to work.

Any help is highly appreciated!

Thanks


回答1:


Use Instant from java.time package (java 8) instead, it should look like below

String value = "2017-11-23T14:00:49.184000000Z";
Instant instant = Instant.parse(value);
Date date = Date.from(instant);
System.out.println(date);



回答2:


you can use timeZone as well like this as another solution.

TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
            Calendar cal = Calendar.getInstance(tz);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
            sdf.setCalendar(cal);
            cal.setTime(sdf.parse("2017-11-23T14:58:00.184000000Z"));
            Date date = cal.getTime();
            System.out.println(date);


来源:https://stackoverflow.com/questions/47926442/java-simpledateformat-pattern-parseexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!