java计算上个月最后一天是几号_java获得当前日期的上个月的最后一天的日期&&每月的最大天数...

// 获得当前时间所在月份的上个月的最后一天所在日期

public String getLastMonthDay(Timestamp t) {

Calendar cal = Calendar.getInstance();

Date date = t;

cal.setTime(date);

int year = 0;

int month = cal.get(Calendar.MONTH); // 上个月月份

//设置年月

if (month == 0) {

year = cal.get(Calendar.YEAR) - 1;

month = 12;

} else {

year = cal.get(Calendar.YEAR);

}

//设置天数

String temp=year + "-" + month ;

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

Date d = null;

try {

d = format.parse(temp);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

cal.setTime(d);

int day =cal.getActualMaximum(Calendar.DAY_OF_MONTH);

String endDay = year + "-" + month + "-" + day;

return endDay;

}

//得到某月的天数

public int getMonthDay(String source){

//String source = "2007年12月";

int count=30;

SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");

try {

Date date = format.parse(source);

Calendar calendar = new GregorianCalendar();

calendar.setTime(date);

count=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

}catch (Exception e) {

e.printStackTrace();

}

return count;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值