自己写的日期处理类--月份的第一天和最后一天

自己写的日期处理类--月份的第一天和最后一天


import  java.util.Calendar;
import  java.util.Date;
import  java.util.GregorianCalendar;

public   class  DoDate  {

    
//当前月份的第一天
    public static String firstDay(){
        java.text.SimpleDateFormat   df   
=   new   java.text.SimpleDateFormat("yyyy-MM-dd");
         GregorianCalendar gc 
= (GregorianCalendar) Calendar.getInstance();   
            gc.setTime(
new Date());   
            gc.set(Calendar.DAY_OF_MONTH, 
1);                  
            String day_first 
= df.format(gc.getTime())+" 00:00:00"
            
return day_first;
    }

    
//当前月份的最后一天
    public static String lastDay(){
        Calendar cal
=Calendar.getInstance();
        cal.add(   cal.MONTH,
1   );      
        cal.set(cal.DATE,
1);      
        cal.add(cal.DATE,
-1);     
        java.text.SimpleDateFormat   df   
=   new   java.text.SimpleDateFormat("yyyy-MM-dd");      
        String day_end
=df.format(cal.getTime())+" 23:59:59";  
        
return day_end;
    }

    
//距离当前月份N个月的第一天
    public static String firstDay(int n){    
        java.text.SimpleDateFormat   df   
=   new   java.text.SimpleDateFormat("yyyy-MM-dd");
         GregorianCalendar gc 
= (GregorianCalendar) Calendar.getInstance();   
            gc.setTime(
new Date()); 
            gc.set(Calendar.MONTH, gc.get(Calendar.MONTH)
+n);
            gc.set(Calendar.DAY_OF_MONTH, 
1);                  
            String day_first 
= df.format(gc.getTime())+" 00:00:00"
            
return day_first;
    }

    
//距离当前月份N个月的最后一天
    public static String lastDay(int n){
        Calendar cal
=Calendar.getInstance();
        cal.add(cal.MONTH,
1);
        cal.set(cal.MONTH, cal.get(Calendar.MONTH)
+n);
        cal.set(cal.DATE,
1);      
        cal.add(cal.DATE,
-1);     
        java.text.SimpleDateFormat   df   
=   new   java.text.SimpleDateFormat("yyyy-MM-dd");      
        String day_end
=df.format(cal.getTime())+" 23:59:59";  
        
return day_end;
    }

}

你可能感兴趣的:(自己写的日期处理类--月份的第一天和最后一天)