我如何格式化它以打印月份名称而不是数字?
How can I format this to print the month name instead of number?
这是我目前拥有的一段代码...我需要它来打印 June 8, 2008 而不是 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) + " ";
}
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
是四位数年份。
这是我目前拥有的一段代码...我需要它来打印 June 8, 2008 而不是 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) + " ";
}
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
是四位数年份。