简体   繁体   English

日期格式设置不正确

[英]Date formatting is not formatting correctly

I can't figure out why this is returning Wed Jul 02 18:21:27 CDT 2014 instead of 07/02/14 6:21 pm 我不知道为什么这会返回CDT 2014年7月2日星期三18:21:27而不是2014年7月2日下午6:21

pubdate = Mon, 30 Jun 2014 22:37:15 +0000

public void setPubDate(String pubDate) {

    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
    long x = dateFormat.parse(pubDate).getTime();
    Date date = new Date(x);
    SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yy H:mm aa");
    newFormat.format(dateFormat.parse(pubDate));
    this.pubDate = date;

}
this.pubDate = date;//Assign the reference of date Object
//this.pubDate will have value of date NOT Format :)

But here format won't be passed to pubDate as that will remain as it is. 但是这里的格式不会传递给pubDate ,因为它将保持原样。 If you want to make your pubDate to have dd/Mm/yyyy aa format you have to format the pubDate as well here you are only assigning reference from one date to other but formation on one date won't affect the other one you have to apply that to this.pubDate whenever you want to use pubDate . 如果要使pubDate具有dd/Mm/yyyy aa格式,则还必须对pubDate进行格式化,在这里,您仅将一个日期的参考分配给另一个日期,但是一个日期的格式不会影响另一个日期每当您要使用pubDate时,都将其应用于this.pubDate

You can declare general format(Class level Object) and use it in your program whenever you want to display the date. 您可以声明通用格式(类级别对象),并在需要显示日期时在程序中使用它。

如果要打印所需的格式,则必须使用String表示日期,否则,日期类型将始终打印此格式“ dow mon dd hh:mm:ss zzz yyyy”

Because Date has toString() which per the Javadoc, 由于Date具有每个Javadoc的toString()

Converts this Date object to a String of the form: 将此Date对象转换为以下形式的字符串:

 dow mon dd hh:mm:ss zzz yyyy 

where: 哪里:

 dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat). mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec). dd is the day of the month (01 through 31), as two decimal digits. hh is the hour of the day (00 through 23), as two decimal digits. mm is the minute within the hour (00 through 59), as two decimal digits. ss is the second within the minute (00 through 61, as two decimal digits. zzz is the time zone (and may reflect daylight saving time). Standard time zone abbreviations include those recognized by the method parse. If time zone information is not available, then zzz is empty - that is, it consists of no characters at all. yyyy is the year, as four decimal digits. 

When you want to deviate from that, you will need your newFormat - 如果您希望newFormat ,则需要使用newFormat

 // As a String
System.out.println(newFormat.format(dateFormat.parse(pubDate)));
public void setPubDate(String pubDate) {

  SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
  long x = dateFormat.parse(pubDate).getTime();
  Date date = new Date(x);
  SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yy H:mm aa");
  return newFormat.format(dateFormat.parse(pubDate));

}

Use corrected code below: 在下面使用更正的代码:

public void setPubDate(String pubDate) {

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
long x = dateFormat.parse(pubDate).getTime();
Date date = new Date(x);
SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yy H:mm aa");
System.out.println("Formatted date is ="+ newFormat.format(x));

}

Try this, Create your custom date class 试试这个,创建您的自定义日期类

public class MyDate extends Date
{
    @Override
    public String toString() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy hh:mm aa");
        return dateFormat.format(new Date());           
    }
}

then print the object like 然后像打印对象

System.out.println(new MyDate()); System.out.println(new MyDate());

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

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