android - java.text.ParseException: Unparseable date for "hh:mm a" -
this question has answer here:
i struggling converting date "hh:mm a" "hh:mm" in android. while don't errors on simple java application, error on android. here code:
string time = "02:00 pm"; string formattedtime = ""; simpledateformat displayformat = new simpledateformat("hh:mm"); string parseformats[] = new string[]{"hh:mm", "hhmm", "hh:mm a", "hh a"};  (string parseformat : parseformats) {     simpledateformat formatting = new simpledateformat(parseformat);     try {         date date = formatting.parse(time);         formattedtime = displayformat.format(date);         system.out.println(formattedtime);     } catch (parseexception e) {         system.out.println(parseformat);         e.printstacktrace();     } } in case of java expected:
02:00 hhmm java.text.parseexception: unparseable date: "02:00 pm"     @ java.text.dateformat.parse(dateformat.java:366)     @ helloworld.main(helloworld.java:31) 14:00 hh java.text.parseexception: unparseable date: "02:00 pm"     @ java.text.dateformat.parse(dateformat.java:366)     @ helloworld.main(helloworld.java:31) in android application same code returns exception "hh:mm a" well:
i/system.out: hh:mm w/system.err: java.text.parseexception: unparseable date: "02:00 pm" imports same:
import java.text.parseexception; import java.text.simpledateformat; import java.util.date; in case of java app, succeeds "hh:mm" , "hh:mm a". in case of android, succeeds "hh:mm".
found problem @arnauddenoyelle. checked simpledateformat class, turns out 1 valued constructor calls 2 valued defaul locale:
public simpledateformat(string pattern)     {         this(pattern, locale.getdefault(locale.category.format));     } am/pm seen error country, uses 24 hour system. phone , simple java application return different default locale values, getting different results.
while not solution, used locale.france avoid problem:
simpledateformat formatting = new simpledateformat(parseformat, locale.france); 
Comments
Post a Comment