简体   繁体   中英

Cannot parse SimpleDateFormat

I am using this code to parse a Date.

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat formatterParse = new SimpleDateFormat("yyyy-MM-dd");
String s = formatter.format(formatterParse.parse(m.get("lastModifiedDate")));

m is a HashMap and someModifiedDate is coming as 2010-11-26

EDIT: I get InvalidTypeEception. java. Genereated value (java.sql.Date) incompatible with java.lang.String

The snippet below works fine.

Map<String, String> m = new HashMap<String, String>() {{
    put("lastModifiedDate", "2010-11-26");
}};

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat formatterParse = new SimpleDateFormat("yyyy-MM-dd");
String s = formatter.format(formatterParse.parse(m.get("lastModifiedDate")));

System.out.println(s);

The output is 11/26/2010 , thus either

  1. ...you expected something different ( what? ) in which case you need to revise your code, or
  2. ...something is wrong in your code elsewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM