简体   繁体   中英

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

This is a piece of the code I have so far... I need it to print out June 8, 2008 instead of 6/8/2014. How can I do this? If you need to see more of the other code, just ask.

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

    }

}

Edit: I see other posts that talk about DateFormatSymbols, but how would I implement it?

Edit Edit: Here's the code that someone asked for...

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

You could use a SimpleDateFormat and something like

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

The four M characters become the full-name of the month. The single d is the day of the month and yyyy is a four digit year.

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