Android SimpleDateFormat format issue

2019-03-06 10:28发布

I am trying to format a date into required format. but i receive wrong date out put. here is what i am doing.

I receive the dates as following:

3/24/2012 8:25:03 AM
3/23/2012 2:57:01 PM
3/15/2012 9:28:01 AM

And want to show them as

Sun, Mar 24
Sat, Mar 23
Tue, Mar 15

Code I am using is as follows:

SimpleDateFormat sourceFormat = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
SimpleDateFormat desiredFormat = new SimpleDateFormat("EE, MMM dd");

Date dt = sourceFormat.parse("3/24/2012 8:25:03 AM");
String desiredDateString1 = desiredFormat.format(dt)

dt = sourceFormat.parse("3/23/2012 2:57:01 PM");
String desiredDateString2 = desiredFormat.format(dt)

dt = sourceFormat.parse("3/15/2012 9:28:01 AM");
String desiredDateString3 = desiredFormat.format(dt)

But instead of getting correct output, i get

desiredDateString1 = "Tue, Jan 24"
desiredDateString2 = "Mon, Jan 23"
desiredDateString3 = "Sun, Jan 15"

It Conciders the month as Jan instead of Mar ... y? Thanks in advance

1条回答
可以哭但决不认输i
2楼-- · 2019-03-06 10:57
SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");

mm means minute in the hour. you should use MM

try with

SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
查看更多
登录 后发表回答