java计算年度剩余天数
输入日期 计算当年天数
public static int parseRemainDays(String strDate){
if(strDate==null || strDate.isEmpty() || strDate.length()<10){
return -1;
}
SimPleDateFormat sm = new SimPleDateFormat(strDate.length()>18?"yyyy-MM-dd HH:mm:ss":"yyyy-MM-dd");
Date date=null;
try{
date=sm.parse(strDate);
}catch(ParseException ex){
ex.printStackTrace();
return -1;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_YEAR) - calendar.get(Calendar.DAY_OF_YEAR)
}
该博客内容展示了如何使用Java编程来计算输入日期当年的剩余天数。通过SimpleDateFormat解析日期,然后利用Calendar获取当年的最大天数和当前天数进行相减,得到剩余天数。代码中包含了错误处理和日期格式的判断。
707

被折叠的 条评论
为什么被折叠?



