JAVA獲取每個月的天數
阿新 • • 發佈:2019-02-05
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class CalendarTester {
private static SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
public static void main(String[] args) {
String source = "2007年12月";
try {
Date date = format.parse(source);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class CalendarTester {
private static SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
public static void main(String[] args) {
String source = "2007年12月";
try {
Date date = format.parse(source);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
} catch (ParseException e) {
e.printStackTrace();
}
}
}