在Android中将日期格式转换为AM / PM时输出错误

Error in output while converting date format to AM/PM in Android

我将从服务器收到的日期转换为yyyy-mm-dd hh:mm:ss到dd-mm-yyyy hh:mm:ss am / pm

我写下面的代码来做..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;

一切正常,除非你注意到控制台日志下面06:21:48 am,06:09:44 am,其他几个人显示AM而不是PM。

我的代码是错的吗? 或者有任何错误?

1
2
3
4
5
6
7
8
9
10
11
12
13
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


str_date是24小时格式

H小时(0-23),
h上午/下午(1-12)

1
2
 SimpleDateFormat inputFormat = new SimpleDateFormat(
   "yyyy-MM-dd HH:mm:ss", Locale.getDefault());


实际上你的代码中的日期转换正确如下

输入日期:2015-04-02 13:10:19 // GMT + 00:00

转换日期:02-04-2015,01:10:19 PM // GMT + 00:00

现在转换为您的设备默认本地,因为您设置
outputFormat.setTimeZone(TimeZone.getDefault());

输出日期:02-04-2015,06:40:19 pm //使用您的设备Locale。 我认为您的设备GMT + 05:30