同学叫我帮他写个小util类,我以前也没弄过,写了先存这儿,以后估计自己也得用。

 public abstract class DateUtils ...{
public abstract class DateUtils ...{

 /** *//**
    /** *//** * 取得日期所在周的第一天
     * 取得日期所在周的第一天 * @param date
     * @param date * @return
     * @return */
     */
 public static Date getFirstWeekDay(Date date) ...{
    public static Date getFirstWeekDay(Date date) ...{ initCalendar(date);
    initCalendar(date); gc.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    gc.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return gc.getTime();
    return gc.getTime(); }
    } 
    
 /** *//**
    /** *//** * 取得日期所在周的最后一天
     * 取得日期所在周的最后一天 * @param date
     * @param date * @return
     * @return */
     */
 public static Date getLastWeekDay(Date date) ...{
    public static Date getLastWeekDay(Date date) ...{ initCalendar(date);
    initCalendar(date); gc.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    gc.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); return gc.getTime();
    return gc.getTime(); }
    } 
 
 /** *//**
    /** *//** * 取得日期所在月的最后一天
     * 取得日期所在月的最后一天 * @param date
     * @param date * @return
     * @return */
     */
 public static Date getFirstMonthDay(Date date) ...{
    public static Date getFirstMonthDay(Date date) ...{ initCalendar(date);
    initCalendar(date); int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
    int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH); gc.add(Calendar.DAY_OF_MONTH, 1 - dayOfMonth);
    gc.add(Calendar.DAY_OF_MONTH, 1 - dayOfMonth); return gc.getTime();
    return gc.getTime(); }
    } 
    
 /** *//**
    /** *//** * 取得日期所在月的最后一天
     * 取得日期所在月的最后一天 * @param date
     * @param date * @return
     * @return */
     */
 public static Date getLastMonthDay(Date date) ...{
    public static Date getLastMonthDay(Date date) ...{ initCalendar(date);
    initCalendar(date); int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
    int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH); int maxDaysOfMonth = gc.getActualMaximum(Calendar.DAY_OF_MONTH);
    int maxDaysOfMonth = gc.getActualMaximum(Calendar.DAY_OF_MONTH); gc.add(Calendar.DAY_OF_MONTH, maxDaysOfMonth - dayOfMonth);
    gc.add(Calendar.DAY_OF_MONTH, maxDaysOfMonth - dayOfMonth); return gc.getTime();
    return gc.getTime(); }
    } 
    
 /** *//**
    /** *//** * 取得日期所在旬的最后一天
     * 取得日期所在旬的最后一天 * @param date
     * @param date * @return
     * @return */
     */
 public static Date getFirstTenDaysDay(Date date) ...{
    public static Date getFirstTenDaysDay(Date date) ...{ initCalendar(date);
    initCalendar(date); int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
    int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
 if(dayOfMonth <= 10) ...{
    if(dayOfMonth <= 10) ...{ gc.set(Calendar.DAY_OF_MONTH, 1);
        gc.set(Calendar.DAY_OF_MONTH, 1);
 } else if(dayOfMonth > 20) ...{
    } else if(dayOfMonth > 20) ...{ gc.set(Calendar.DAY_OF_MONTH, 21);
        gc.set(Calendar.DAY_OF_MONTH, 21);
 } else ...{
    } else ...{ gc.set(Calendar.DAY_OF_MONTH, 11);
        gc.set(Calendar.DAY_OF_MONTH, 11); }
    } return gc.getTime();
    return gc.getTime(); }
    } 
    
 /** *//**
    /** *//** * 取得日期所在旬的最后一天
     * 取得日期所在旬的最后一天 * @param date
     * @param date * @return
     * @return */
     */
 public static Date getLastTenDaysDay(Date date) ...{
    public static Date getLastTenDaysDay(Date date) ...{ initCalendar(date);
    initCalendar(date); int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
    int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
 if(dayOfMonth <= 10) ...{
    if(dayOfMonth <= 10) ...{ gc.set(Calendar.DAY_OF_MONTH, 10);
        gc.set(Calendar.DAY_OF_MONTH, 10);
 } else if(dayOfMonth > 20) ...{
    } else if(dayOfMonth > 20) ...{ gc.set(Calendar.DAY_OF_MONTH, gc.getActualMaximum(Calendar.DAY_OF_MONTH));
        gc.set(Calendar.DAY_OF_MONTH, gc.getActualMaximum(Calendar.DAY_OF_MONTH));
 } else ...{
    } else ...{ gc.set(Calendar.DAY_OF_MONTH, 19);
        gc.set(Calendar.DAY_OF_MONTH, 19); }
    } return gc.getTime();
    return gc.getTime(); }
    } 
    
 private static void initCalendar(Date date) ...{
    private static void initCalendar(Date date) ...{
 if(date == null) ...{
    if(date == null) ...{ throw new IllegalArgumentException("argument date must be not null");
        throw new IllegalArgumentException("argument date must be not null"); }
    } 
     gc.clear();
    gc.clear(); gc.setTime(date);
    gc.setTime(date); }
    } 
     private static GregorianCalendar gc = null;
    private static GregorianCalendar gc = null;
 static ...{
    static ...{ gc = new GregorianCalendar(Locale.CHINA);
    gc = new GregorianCalendar(Locale.CHINA); gc.setLenient(true);
    gc.setLenient(true); gc.setFirstDayOfWeek(Calendar.MONDAY);
    gc.setFirstDayOfWeek(Calendar.MONDAY); }
    } 
    
 /** *//**
    /** *//** * @param args
     * @param args */
     */
 public static void main(String[] args) ...{
    public static void main(String[] args) ...{ Calendar c = Calendar.getInstance();
    Calendar c = Calendar.getInstance(); //    c.set(Calendar.MONTH, 7);
//    c.set(Calendar.MONTH, 7); //    c.set(Calendar.DAY_OF_MONTH, 9);
//    c.set(Calendar.DAY_OF_MONTH, 9); Date date = c.getTime();
    Date date = c.getTime(); 
     System.out.println("getFirstWeekDay = " + getFirstWeekDay(date));
    System.out.println("getFirstWeekDay = " + getFirstWeekDay(date)); System.out.println("getLastWeekDay = " + getLastWeekDay(date));
    System.out.println("getLastWeekDay = " + getLastWeekDay(date)); System.out.println("getFirstMonthDay = " + getFirstMonthDay(date));
    System.out.println("getFirstMonthDay = " + getFirstMonthDay(date)); System.out.println("getLastMonthDay = " + getLastMonthDay(date));
    System.out.println("getLastMonthDay = " + getLastMonthDay(date)); System.out.println("getFirstTenDaysDay = " + getFirstTenDaysDay(date));
    System.out.println("getFirstTenDaysDay = " + getFirstTenDaysDay(date)); System.out.println("getLastTenDaysDay = " + getLastTenDaysDay(date));
    System.out.println("getLastTenDaysDay = " + getLastTenDaysDay(date)); }
    } }
} 
                   
                   
                   
                   
                             本文介绍了一个实用的日期工具类,包含获取指定日期所在周、月及旬的开始与结束日期的方法实现。通过这些方法,可以方便地进行日期范围的计算。
本文介绍了一个实用的日期工具类,包含获取指定日期所在周、月及旬的开始与结束日期的方法实现。通过这些方法,可以方便地进行日期范围的计算。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   4173
					4173
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            