java计算两个年月日之间相差的天数:
public static int daysBetween(String smdate,String bdate) throws ParseException{ int daysInterval=0; if(StringUtils.isNoneBlank(smdate)&&StringUtils.isNoneBlank(bdate)){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(smdate)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(bdate)); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); daysInterval = Integer.parseInt(String.valueOf(between_days)); } return daysInterval; }
java计算年月之间的加法和减法:
public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); String time=sdf.format(new Date()); Calendar cd = Calendar.getInstance(); cd.setTime(sdf.parse(time)); cd.add(Calendar.MONTH, 0);//往前一月,,如果是整数则进行加法,如果是负数,则进行减法 Date date=cd.getTime(); String endTimeString = sdf.format(date); cd.add(Calendar.MONTH, -7);//往后7个月 String startTimeString = sdf.format(cd.getTime()); String startTime = startTimeString.replaceAll("-", "");//格式日期yyyyMM String endTime = endTimeString.replaceAll("-", "");//格式日期yyyyMM System.out.println(startTime); System.out.println(endTime); }
获取一个月中的第一天和最后一天:
public static void main(String[] args) { // 获取当前年份、月份、日期 Calendar cale = null; cale = Calendar.getInstance(); // 获取当月第一天和最后一天 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // 获取前月的第一天 cale = Calendar.getInstance(); cale.add(Calendar.MONTH, -6); cale.set(Calendar.DAY_OF_MONTH, 1); String startTimeString = format.format(cale.getTime()); // 获取前月的最后一天 cale = Calendar.getInstance(); cale.add(Calendar.MONTH, 0); cale.set(Calendar.DAY_OF_MONTH, 0); String endTimeString = format.format(cale.getTime()); String startTime =startTimeString.replaceAll("-", "");//格式日期yyyyMMdd String endTime =endTimeString.replaceAll("-", "");//格式日期yyyyMMdd String layerTime = startTimeString +"至"+endTimeString; System.out.println(layerTime); }
计算两个月份之间相差的月份个数:
public int getMonthInterval(String str1,String str2) throws ParseException{ int monthLength = 0; if(StringUtils.isNoneBlank(str1)&& StringUtils.isNoneBlank(str2)){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar bef = Calendar.getInstance(); Calendar aft = Calendar.getInstance(); bef.setTime(sdf.parse(str1)); aft.setTime(sdf.parse(str2)); int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH); int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12; monthLength = Math.abs(month + result); } return monthLength; }
java对当前月份进行传值计算:
public static Map getMonth(int length) throws ParseException{ Map map = new HashMap(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); String time=sdf.format(new Date()); Calendar cd = Calendar.getInstance(); cd.setTime(sdf.parse(time)); cd.add(Calendar.MONTH, -length);//往前一月 Date date=cd.getTime(); String monthFormat= sdf.format(date); // System.out.println(monthFormat); String monthString =monthFormat.replaceAll("-", "");//格式日期yyyyMM map.put("monthFormat", monthFormat); map.put("monthString", monthString); return map; }
到此这篇关于java中年月日的加减法使用示例的文章就介绍到这了,更多相关java中年月日的加减法内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!