
用java calender来计算某年某月的最大天数,但是获得的日期总是来回变,不知道为什么
Datedate=newDate();SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-dd");Stringyea...
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String yearMthDay = format.format(date);
String[] ymd = yearMthDay.split("-");
int year = Integer.parseInt(ymd[0]);
Calendar cle = Calendar.getInstance();
cle.add(Calendar.YEAR, 0);
cle.set(Calendar.MONTH, 1);
String evalBeginDate = (year - 1) + "-" + 3+ "-01";
String evalEndDate = (year) + "-" + 2+ "-" + cle.getActualMaximum(Calendar.DATE);
之前老变弄的年都不从calender那直接获得了(所以上面的代码看着有点怪),但是只从calender里获得月也老是变,不知道为什么。 展开
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String yearMthDay = format.format(date);
String[] ymd = yearMthDay.split("-");
int year = Integer.parseInt(ymd[0]);
Calendar cle = Calendar.getInstance();
cle.add(Calendar.YEAR, 0);
cle.set(Calendar.MONTH, 1);
String evalBeginDate = (year - 1) + "-" + 3+ "-01";
String evalEndDate = (year) + "-" + 2+ "-" + cle.getActualMaximum(Calendar.DATE);
之前老变弄的年都不从calender那直接获得了(所以上面的代码看着有点怪),但是只从calender里获得月也老是变,不知道为什么。 展开
2个回答
展开全部
System.out.println(getMaxDay(2013,2));
public static int getMaxDay(int year,int month){
Calendar c = Calendar.getInstance();
c.set(year, month,1);
c.add(Calendar.DAY_OF_YEAR, -1);
return c.get(Calendar.DAY_OF_MONTH);
}
public static int getMaxDay(int year,int month){
Calendar c = Calendar.getInstance();
c.set(year, month,1);
c.add(Calendar.DAY_OF_YEAR, -1);
return c.get(Calendar.DAY_OF_MONTH);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询