Date localization in java using DateFormat.getDateInstance -


i have date must localized. below code returns 5/1/12 19:06:34 result want 05/01/12 19:06:34 please tell me how manage this.

  private string localizedate(string date){ //format 2012-05-01 19:30:49      locale loc = datacontextholder.getdatacontext().getlocale();     simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss", loc);      date parsed=null;     try {         parsed = formatter.parse(date);     } catch (parseexception e) {         // todo auto-generated catch block         e.printstacktrace();     }     dateformat df = dateformat.getdateinstance(dateformat.short, loc);      string localizeddate = df.format(parsed) + " " + date.substring(11, 13) + ":"             + date.substring(14, 16) + ":"             + date.substring(17, 19);      return localizeddate; } 

you can avoid leading zeros reducing number of consecutive pattern letters particular element. multiple pattern letters in row tell date formatter that, @ minimum, want many characters express value.

in example, following should resolve problem.

new simpledateformat("y-m-d h:m:s", loc); 

find more in the simpledateformat documentation.

for clarity, see following example.

simpledateformat = new simpledateformat("yyyyy-mmmm-dddd hhh:mmmm:sssss"); simpledateformat b = new simpledateformat("y-m-d h:m:s");  system.out.println(a.format(new date()));  // prints 02012-june-0005 012:0027:00026 system.out.println(b.format(new date())); // prints 12-6-5 12:27:26 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -