//万年历 Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE,-1); //改变日期,改变年份、月份类似 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); System.out.println("today is:" + format.format(Calendar.getInstance().getTime()));//格式化现在的时间 System.out.println("yesterday is:" + format.format(cal.getTime()));//格式化改变后的时间 System.out.println(cal.get(Calendar.YEAR));//获得年份 System.out.println(cal.get(Calendar.MONTH));//获得月份 System.out.println(cal.get(Calendar.DATE));//获得日期 System.out.println(cal.get(Calendar.HOUR));//获得小时 System.out.println(cal.get(Calendar.MINUTE));//获得分钟 SimpleDateFormat format1 = new SimpleDateFormat("dd"); System.out.println("today is:" + format1.format(Calendar.getInstance().getTime()));//格式化现在的时间,获得日期 System.out.println("yesterday is:" + format1.format(cal.getTime()));//格式化改变后的时间,获得日期
//JS var date = new Date(); console.log(dateToStr(date)); //2015-12-21 date.setFullYear(date.getFullYear()-1);//年份减1年 date.setMonth(date.getMonth()-1);//月份减1月 date.setDate(date.getDate()-1);//日期减1天 date = new Date(date); console.log(dateToStr(date));//2014-11-20 function dateToStr(date){ //date转成字符串 var str=''; var month=date.getMonth()+1; str=date.getFullYear()+'-'+(month<=9?('0'+month):month)+'-'+(date.getDate()<=9?('0'+date.getDate()):date.getDate()); return str; } var date = new Date(Date.parse($("#start").val().replace(/-/g, "/"))) //把input标签选择的时间转成date