Calendar class application case in Java

 public static Date getNextDay(Date date) {
     Calendar calendar = Calendar.getInstance();
     calendar.setTime(date);
     calendar.add(Calendar.DAY_OF_MONTH, -1);//+1今天的时间加一天
     date = calendar.getTime();
     return date;
 }
//查找上几月任意日期
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     Calendar   cal_1=Calendar.getInstance();//获取当前日期 
    //表示上月最后一天
/*   cal_1.add(Calendar.MONTH, 0);//
     cal_1.set(Calendar.DAY_OF_MONTH,0);//
*/   
    //表示上月第一天
     cal_1.add(Calendar.MONTH, -1);//
     cal_1.set(Calendar.DAY_OF_MONTH,1);//
     String time = sdf.format(cal_1.getTime());
     System.out.println("-----1------firstDay:"+time);
     System.out.println("-----1------firstDay:"+cal_1.getTime());
//查找上周日
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     Calendar cal = Calendar.getInstance(); 
     String str = "2017-05-01";  
     Date d = sdf.parse(str);
     cal.setTime(d);  
     cal.setTime(new Date());  
     // 获得当前日期是一个星期的第几天  
     int daym = cal.get(Calendar.DAY_OF_MONTH);
     int dayWeek = cal.get(Calendar.DAY_OF_WEEK);  
     if (1 == dayWeek) {  
         cal.add(Calendar.DAY_OF_MONTH,0); 
         System.out.println("yes");
     }else{  
         // 设置一个星期的第一天
         cal.setFirstDayOfWeek(Calendar.SUNDAY);  
         // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值  
         cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);  
         System.out.println("no");
     }
     cal.getTime();
     System.out.println("周日:" + sdf.format(cal.getTime()));
Published 137 original articles · Like 123 · Visit 250,000+

Guess you like

Origin blog.csdn.net/lz20120808/article/details/78864167