簡體   English   中英

我該如何格式化以打印月份名稱而不是數字?

[英]How can I format this to print the month name instead of number?

這是我到目前為止的一部分代碼...我需要它才能在2008年6月8日而不是2014年6月8日打印出來。 我怎樣才能做到這一點? 如果您需要查看更多其他代碼,請詢問。

    public void weatherRecord(int month, int date) {

    for (int loc = 0; loc < weatherRecord.length; loc++) {
        System.out.print("Location: " + weatherRecord[loc][month][date].getLocation() + "\t\t");
        System.out.print("Date: " + weatherRecord[loc][month][date+1].getDateToString() + "\n");
        System.out.print("High Temp: " + weatherRecord[loc][month][date].getHighTemp() + "\t\t\t");
        System.out.print("Low Temp: " + weatherRecord[loc][month][date].getLowTemp() + "\n");
        System.out.print("Avg Wind: " + weatherRecord[loc][month][date].getAvgWind() + "\t\t\t");
        System.out.print("Max Wind: " + weatherRecord[loc][month][date].getMaxWind() + "\n");
        System.out.print("Precipitation: " + weatherRecord[loc][month][date].getPrecip() + " inches.\n\n");

    }

}

編輯:我看到其他談論DateFormatSymbols的帖子,但是我將如何實現呢?

編輯編輯:這是某人要求的代碼...

public String getDateToString() {
    return "" + this.date.get(Calendar.MONTH) + "/"
            + this.date.get(Calendar.DAY_OF_MONTH) + "/"
            + this.date.get(Calendar.YEAR) + " ";
}
import java.text.DateFormatSymbols;

public String getDateToString() {
    return "" + monthNumToName(this.date.get(Calendar.MONTH)) + " "
            + this.date.get(Calendar.DAY_OF_MONTH) + ", "
            + this.date.get(Calendar.YEAR) + " ";
}

private String monthNumToName(int month) {  
    return new DateFormatSymbols().getMonths()[month-1];
}

您可以使用SimpleDateFormat東西

private static final DateFormat SDF = new SimpleDateFormat("MMMM d, yyyy");
public String getDateToString() {
    return SDF.format(date.getTime());
}

這四個M字符成為月份的全名。 單個d是月份中的日期,而yyyy是四位數的年份。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM