2014-07-08 246 views
0

我想不通这是为什么返回 周三7月2 18:21:27 CDT 2014 而不是 14年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; 

} 
+2

可以发布什么是pubDate值? – KOTIOS

+0

请发布pubDate值.... –

+0

可能重复[是否有可能获得特定格式的java.util.Date对象?](http://stackoverflow.com/questions/19207477/is-it-possible-to -get-java的util的最新对象与 - 特定格式) –

回答

1

如果你要打印你想要的格式,你必须使用字符串来表示你的约会,否则,日期类型将始终打印格式“陶氏周一DD HH:MM:SS ZZZ YYYY”

0

因为Date具有toString()其中每Javadoc中,

此日期对象转换为以下形式的字符串:

dow mon dd hh:mm:ss zzz yyyy 

其中:

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. 

当你想从偏离,你将需要你的newFormat -

// As a String 
System.out.println(newFormat.format(dateFormat.parse(pubDate))); 
0
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)); 

} 
0

使用更正下面的代码:

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

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

但是这里的格式不会传递到pubDate,因为这样会保持原样。 如果你想让你的pubDate拥有dd/Mm/yyyy aa格式,你也必须格式化pubDate这里你只是将参考从一个日期分配到另一个日期,但是在一个日期中的编号不会影响另一个你必须将其应用到this.pubDate每当你想使用pubDate

您可以声明通用格式(Class level Object),并在您想要显示日期的程序中使用它。

0

尝试这一点,创建自定义的日期类

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

然后打印的物体,像

的System.out.println(新指明MyDate());