简体   繁体   中英

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

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. 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 .

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,

Converts this Date object to a String of the form:

 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 -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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