如何格式化以打印月份名称而不是数字?

时间:2015-04-13 01:36:23

标签: java format

这是我到目前为止的一段代码......我需要它打印出2008年6月8日而不是6/8/2014。我怎样才能做到这一点?如果您需要查看更多其他代码,请询问。

    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) + " ";
}

2 个答案:

答案 0 :(得分:1)

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];
}

答案 1 :(得分:1)

您可以使用SimpleDateFormat

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

四个M个字符成为当月的全名。单个d是一个月中的某一天,yyyy是四位数年份。