时间和日期的查找

查找两天前的日期:

Date dNow = new Date(); //当前时间
Date dBefore = new Date();
Calendar calendar
= Calendar.getInstance(); //得到日历 calendar.setTime(dNow);//把当前时间赋给日历 dNow= calendar.getTime(); //得到当前时间 calendar.add(Calendar.DAY_OF_MONTH, -2); //设置为前两天 dBefore = calendar.getTime(); //得到前两天的时间 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式 String defaultStartDate = sdf.format(dBefore); //格式化前两天 String defaultEndDate = sdf.format(dNow); //格式化当前时间 System.out.println("前2天的时间是:" + defaultStartDate); System.out.println("当前时间是:" + defaultEndDate);

猜你喜欢

转载自www.cnblogs.com/dztHome/p/8929838.html