繁体   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