java - How to format the given time string and convert to date/time object -


i getting time object in form of string rest service . need extract time , time operation.the given time string "2015-06-16t14:58:48z". tried below code , convert string time , , getting incorrect values.

    string time = "2015-06-16t14:58:48z";      simpledateformat formatter = new simpledateformat("yyyy-mm-dd't'hh:mm:ss'z'", locale.us);      string dateinstring = "2015-06-16t14:58:48z";      date date = formatter.parse(dateinstring);     system.out.println("original string : " + time);     system.out.println("after converting time : " + formatter.format(date)); 

the output getting below: original string : 2015-06-16t14:58:48z after converting time : 2015-12-362t02:58:48z

the converted date somehow getting wrong value.please suggest mistake.thanks.

you format string has couple of mistakes:

  • y means week year, not year, y
  • d means day of year. should have used d, means day of month.
  • h means 12-hour notation time of day. since have 14 should use h, handle 24-hour notation.

to sum up:

simpledateformat formatter =      new simpledateformat("yyyy-mm-dd't'hh:mm:ssx", locale.us); 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -