int m=c.get(c.MONTH)+1;

来源:百度知道 编辑:UC知道 时间:2024/05/30 03:16:20
import java.util.*;
class Day {
public static void main(String[] args){
Calendar c= Calendar.getInstance();
System.out.print("今天的日期是:");
int y=c.get(c.YEAR);
int m=c.get(c.MONTH)+1;
int d=c.get(c.DATE);
System.out.println(y+"-"+m+"-"+d);
}
}
int m=c.get(c.MONTH)+1;这后面为什么要加1啊

看看Calendar源代码:

package java.util;

/**
* Calendar is an abstract base class for converting between
* a Date object and a set of integer fields such as
* YEAR, MONTH, DAY, HOUR,
* and so on.
...................................
* Subclasses of Calendar interpret a Date
* according to the rules of a specific calendar system. The platform
* provides one concrete subclass of Calendar:
* GregorianCalendar. Future subclasses could represent
* the various types of lunar calendars in use in many parts of the world.
......................................................
*/

public abstract class Calendar implements Serializable, Cloneable {