Bootstrap

Java日期加减

 
DateTime newDate = new Date();    
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");                        
//获取当前时间
String day = dateFormat.format(newDate);
Calendar calendar = Calendar.getInstance();                                
calendar.setTime(newDate); 
calendar.add(Calendar.DAY_OF_MONTH, -1); //当前时间减去一天,即一天前的时间 
String beforeday = dateFormat.format(calendar.getTime());
 
//calendar.add(Calendar.YEAR, 1);//增加一年
    
//calendar.add(Calendar.DATE, 1);//增加一天  
   
//calendar.add(Calendar.DATE, -10);//减10天  
   
//calendar.add(Calendar.MONTH, 1);//增加一个月   
 Calendar cal = Calendar.getInstance();
    // 赋值时年月日时分秒常用的6个值,注意月份下标从0开始,所以取月份要+1
    System.out.println("年:" + cal.get(Calendar.YEAR));
    System.out.println("月:" + (cal.get(Calendar.MONTH) + 1));       
    System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH));
    System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY));
    System.out.println("分:" + cal.get(Calendar.MINUTE));
    System.out.println("秒:" + cal.get(Calendar.SECOND));

;