2

i am converting my date received from server yyyy-mm-dd hh:mm:ss to dd-mm-yyyy hh:mm:ss am/pm

i wrote following code to do it..

SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault());
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy, hh:mm:ss a", Locale.getDefault());

 String out = "";
 inputFormat.setTimeZone(TimeZone.getTimeZone("UTC")); //Also tried GMT+00:00

 Date date = inputFormat.parse(str_date);

 outputFormat.setTimeZone(TimeZone.getDefault());
 out = outputFormat.format(date);

 L.c("The date format for " + str_date + " is " + out);

 return out;

everything works fine except if you note below console log 06:21:48 am, 06:09:44 am and few others shows AM instead of PM.

Is my code wrong? or there any bug?

04-02 19:13:37.381    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 13:10:19 is 02-04-2015, 06:40:19 pm
04-02 19:13:37.414    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 13:02:22 is 02-04-2015, 06:32:22 pm
04-02 19:13:37.436    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 12:51:48 is 02-04-2015, 06:21:48 am
04-02 19:13:42.191    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 12:39:44 is 02-04-2015, 06:09:44 am
04-02 19:13:43.484    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 12:17:48 is 02-04-2015, 05:47:48 am
04-02 19:13:44.990    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 12:10:30 is 02-04-2015, 05:40:30 am
04-02 19:13:56.347    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 12:00:00 is 02-04-2015, 05:30:00 am
04-02 19:13:57.228    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 11:58:13 is 02-04-2015, 05:28:13 pm
04-02 19:13:57.467    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 11:55:18 is 02-04-2015, 05:25:18 pm
04-02 19:13:58.332    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 11:48:13 is 02-04-2015, 05:18:13 pm
04-02 19:13:58.563    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 11:42:28 is 02-04-2015, 05:12:28 pm
04-02 19:14:00.822    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-02 09:54:22 is 02-04-2015, 03:24:22 pm
04-02 19:14:01.009    4815-4815/in.dummy.app D/ARR﹕ The date format for 2015-04-01 11:30:33 is 01-04-2015, 05:00:33 pm
Rafique Mohammed
  • 3,666
  • 2
  • 38
  • 43
  • 1
    Is the String that your trying to parse actually in UTC before parsing String to Date? And you need to parse out the date in 24 hour format `SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());` if you are converting using UTC – kandroidj Apr 02 '15 at 14:05
  • http://stackoverflow.com/questions/23751172/convert-string-to-date-and-time-as-am-pm-format – Yauraw Gadav Apr 02 '15 at 14:10
  • @inner_class7 i guess.. i am receiving it from the server.. its just giving me date as string in "2015-04-02 12:51:48" format – Rafique Mohammed Apr 02 '15 at 14:11
  • @GaurawYadav brother, my question is "Error in ouput" not how to convert date to AM/PM – Rafique Mohammed Apr 02 '15 at 14:16
  • What time zone is the server using? – kandroidj Apr 02 '15 at 15:36

2 Answers2

4

str_date is in 24 hours format

H Hour(0-23), h Hour in am/pm (1-12)

 SimpleDateFormat inputFormat = new SimpleDateFormat(
    "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39
1

Actually date conversion in your code correctly like below

input date : 2015-04-02 13:10:19 // With GMT+00:00

Convert date : 02-04-2015, 01:10:19 PM // With GMT+00:00

Now convert with Your device default local because you set outputFormat.setTimeZone(TimeZone.getDefault());

Output date : 02-04-2015, 06:40:19 pm // With your device Locale. I think your device GMT+05:30

  • Thank you.. I found the solution the problem is i am converting incoming date 24hrs as 12hrs SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()); // it should be HH not hh.. – Rafique Mohammed Apr 03 '15 at 06:24