简体   繁体   English

SimpleDateFormat.parse()使用的日期格式不正确

[英]SimpleDateFormat.parse() not using the correct date format

So I'm trying to parse a string with a date into another format but it's not changing the format. 因此,我试图将带有日期的字符串解析为另一种格式,但它没有更改格式。 The parse function seems to overriding the SimpleDateFormat settings perhaps? 解析功能似乎可以覆盖SimpleDateFormat设置吗?

Date dateOfItem = new SimpleDateFormat("E, d MMMM yyyy", Locale.ENGLISH).parse(item.date);
Log.v("APP", dateOfItem.toString());

item.date for example is this: Tue, 12 May 2015 And thats exactly how I want the format to be in the Date. 例如item.date是这样的: 星期二,2015年5月12日这就是我希望格式出现在Date中的确切方式。 But the Log shows it as: Tue May 12 00:00:00 CDT 2015 which is not the format that I have in the SimpleDateFormat. 但是日志显示为: CDT 2015年5月12日星期二00:00:00 ,这不是我在SimpleDateFormat中使用的格式。

So how can take that string and convert it to a date with the format that I have as the parameter in the SimpleDateFormat? 因此,如何才能将该字符串转换为具有SimpleDateFormat中的参数格式的日期呢?

A Date does not have a format. Date没有格式。 It simply 它只是

represents a specific instant in time, with millisecond precision 代表特定的时间瞬间,精度为毫秒

What you see in your logs is the result of Date#toString() . 您在日志中看到的是Date#toString()的结果。

So either use a SimpleDateFormat to format(Date) your Date object or use the original String value you had. 因此,要么使用SimpleDateFormat format(Date)您的Date对象,要么使用原始的String值。

use this process 使用这个过程

             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);

You are doing the reverse parsing. 您正在执行反向解析。

You have the item.data which is in the E, d MMMM yyyy format and then parse it. 您具有E, d MMMM yyyy格式的item.data,然后对其进行解析。 If you want to print the date in a specified format, you should use SimpleDateFormat#format method. 如果要以指定的格式打印日期,则应使用SimpleDateFormat#format方法。

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

Also check the javadoc 还要检查javadoc

You're starting with a String and putting in a Date . 您以String开头并放入Date And that is all you are using the SimpleDateFormat for. 这就是您使用SimpleDateFormat的全部。

Then you are using the date's default toString() method, so it's not going to apply any custom formatting. 然后,您将使用日期的默认toString()方法,因此不会应用任何自定义格式。

What you need to do is: 您需要做的是:

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

Try this instead: 尝试以下方法:

DateFormat formatter = new SimpleDateFormat("E, d MMMM yyyy", Locale.ENGLISH);
Log.v("APP", formatter.format(dateOfItem));

Using dateOfItem.toString() uses its own output format . 使用dateOfItem.toString()使用其自己的输出格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM