Java SimpleDataFormat.parse()未使用正确的日期格式

Java SimpleDataFormat.parse()未使用正确的日期格式,java,android,simpledateformat,Java,Android,Simpledateformat,所以我试图将一个带有日期的字符串解析成另一种格式,但它并没有改变格式。解析函数似乎覆盖了SimpleDataFormat设置,也许 Date dateOfItem = new SimpleDateFormat("E, d MMMM yyyy", Locale.ENGLISH).parse(item.date); Log.v("APP", dateOfItem.toString()); 例如,item.date为:2015年5月12日星期二 这正是我想要的日期格式。但日志显示为:2015年5月1

所以我试图将一个带有日期的字符串解析成另一种格式,但它并没有改变格式。解析函数似乎覆盖了SimpleDataFormat设置,也许

Date dateOfItem = new SimpleDateFormat("E, d MMMM yyyy", Locale.ENGLISH).parse(item.date);
Log.v("APP", dateOfItem.toString());
例如,item.date为:2015年5月12日星期二 这正是我想要的日期格式。但日志显示为:2015年5月12日星期二00:00:00 CDT,这不是我在SimpleDateFormat中使用的格式

那么,如何将该字符串转换为具有SimpleDataFormat中作为参数的格式的日期呢?

a没有格式。这很简单

表示特定的时间瞬间,精度为毫秒

您在日志中看到的是
Date#toString()
的结果

因此,对
日期
对象使用
SimpleDataFormat
,或者使用原始的
字符串
值。

使用此过程

             DateFormat df = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss E", Locale.getDefault());
              curdate =  df.parse("String date");  
             SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy");
           newFormat = formatter.format(curdate);

您正在执行反向解析

您拥有
E,d MMMM yyyy
格式的item.data,然后对其进行解析。 如果要以指定格式打印日期,应使用SimpleDataFormat#format方法

SimpleDateFormat sdf = new SimpleDateFormat("E, d MMMM yyyy");
Log.v("APP", sdf.format(item.date));

同时检查您从
字符串开始的,并输入
日期。这就是您使用的
SimpleDateFormat
的全部内容

然后您将使用日期的默认
toString()
方法,因此它不会应用任何自定义格式

您需要做的是:

SimpleDateFormat format = new SimpleDateFormat("E, d MMMM yyyy", Locale.ENGLISH);
Date dateOfItem = format.parse(item.date); //string to date
Log.v("APP", format.format(dateOfItem)); //or date to string
请尝试以下方法:

DateFormat formatter = new SimpleDateFormat("E, d MMMM yyyy", Locale.ENGLISH);
Log.v("APP", formatter.format(dateOfItem));
使用
dateOfItem.toString()
使用自己的。

有关更多详细信息和使用Joda时间库的替代方法,请参阅和我的答案。此外,请参阅Joda Time上的命令了解此类格式。