Java日期操作


  1. /** 
  2.  * java日期操作(月末、周末等的日期操作) 
  3.  *  
  4.  * @author  
  5.  *  
  6.  */  
  7. public class DateUtil {  
  8.       
  9.     /** */  
  10.     /** 
  11.      * 取得某天相加(减)後的那一天 
  12.      *  
  13.      * @param date 
  14.      * @param num 
  15.      *            (可正可负) 
  16.      * @return 
  17.      */  
  18.     public static Date getAnotherDate(Date date, int num) {  
  19.         Calendar c = Calendar.getInstance();  
  20.         c.setTime(date);  
  21.         c.add(Calendar.DAY_OF_YEAR, num);  
  22.         return c.getTime();  
  23.     }  
  24.   
  25.     /** */  
  26.     /** 
  27.      * 取得某月的的最后一天 
  28.      *  
  29.      * @param year 
  30.      * @param month 
  31.      * @return 
  32.      */  
  33.     public static Date getLastDayOfMonth(int year, int month) {  
  34.         Calendar cal = Calendar.getInstance();  
  35.         cal.set(Calendar.YEAR, year);// 年  
  36.         cal.set(Calendar.MONTH, month - 1);// 月,因为Calendar里的月是从0开始,所以要减1  
  37.         cal.set(Calendar.DATE, 1);// 日,设为一号  
  38.         cal.add(Calendar.MONTH, 1);// 月份加一,得到下个月的一号  
  39.         cal.add(Calendar.DATE, -1);// 下一个月减一为本月最后一天  
  40.         return cal.getTime();// 获得月末是几号  
  41.     }  
  42.   
  43.     /** */  
  44.     /** 
  45.      * 取得某天是一年中的多少周 
  46.      *  
  47.      * @param date 
  48.      * @return 
  49.      */  
  50.     public static int getWeekOfYear(Date date) {  
  51.         Calendar c = new GregorianCalendar();  
  52.         c.setFirstDayOfWeek(Calendar.MONDAY);  
  53.         c.setMinimalDaysInFirstWeek(7);  
  54.         c.setTime(date);  
  55.         return c.get(Calendar.WEEK_OF_YEAR);  
  56.     }  
  57.   
  58.     /** */  
  59.     /** 
  60.      * 取得某天所在周的第一天 
  61.      *  
  62.      * @param date 
  63.      * @return 
  64.      */  
  65.     public static Date getFirstDayOfWeek(Date date) {  
  66.         Calendar c = new GregorianCalendar();  
  67.         c.setFirstDayOfWeek(Calendar.MONDAY);  
  68.         c.setTime(date);  
  69.         c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());  
  70.         return c.getTime();  
  71.     }  
  72.   
  73.     /** */  
  74.     /** 
  75.      * 取得某天所在周的最后一天 
  76.      *  
  77.      * @param date 
  78.      * @return 
  79.      */  
  80.     public static Date getLastDayOfWeek(Date date) {  
  81.         Calendar c = new GregorianCalendar();  
  82.         c.setFirstDayOfWeek(Calendar.MONDAY);  
  83.         c.setTime(date);  
  84.         c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);  
  85.         return c.getTime();  
  86.     }  
  87.   
  88.     /** */  
  89.     /**http://ini.iteye.com/ 
  90.      * 取得某一年共有多少周 
  91.      *  
  92.      * @param year 
  93.      * @return 
  94.      */  
  95.     public static int getMaxWeekNumOfYear(int year) {  
  96.         Calendar c = new GregorianCalendar();  
  97.         c.set(year, Calendar.DECEMBER, 31235959);  
  98.         return getWeekOfYear(c.getTime());  
  99.     }  
  100.     /** 
  101.      * http://ini.iteye.com/ 
  102.      * 获取某一年某一周的日期 
  103.      * @description  
  104.      * @param year 
  105.      * @param week 
  106.      * @return 
  107.      */  
  108.     public static List<String> getWeekDays(int year,int week){  
  109.         List<String> list = new ArrayList<String>();  
  110.           
  111.         Date date =  getFirstDayOfWeek(year,week);  
  112.         SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");  
  113.        
  114.         for (int i = 0; i < 7; i++) {  
  115.             list.add(d.format(date));  
  116.             System.out.println(d.format(date));  
  117.           
  118.             date.setDate(date.getDate()+1);  
  119.         }  
  120.         return list;  
  121.     }  
  122.     /** */  
  123.     /** 
  124.      * 取得某年某周的第一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天 
  125.      *  
  126.      * @param year 
  127.      * @param week 
  128.      * @return 
  129.      */  
  130.     public static Date getFirstDayOfWeek(int year, int week) {  
  131.         Calendar calFirst = Calendar.getInstance();  
  132.         calFirst.set(year, 07);  
  133.         Date firstDate = getFirstDayOfWeek(calFirst.getTime());  
  134.   
  135.         Calendar firstDateCal = Calendar.getInstance();  
  136.         firstDateCal.setTime(firstDate);  
  137.   
  138.         Calendar c = new GregorianCalendar();  
  139.         c.set(Calendar.YEAR, year);  
  140.         c.set(Calendar.MONTH, Calendar.JANUARY);  
  141.         c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));  
  142.   
  143.         Calendar cal = (GregorianCalendar) c.clone();  
  144.         cal.add(Calendar.DATE, (week - 1) * 7);  
  145.         firstDate = getFirstDayOfWeek(cal.getTime());  
  146.   
  147.         return firstDate;  
  148.     }  
  149.   
  150.     /** */  
  151.     /** 
  152.      * 取得某年某周的最后一天 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周, 2009-01-04为 
  153.      * 2008年最后一周的最后一天 
  154.      *  
  155.      * @param year 
  156.      * @param week 
  157.      * @return 
  158.      */  
  159.     public static Date getLastDayOfWeek(int year, int week) {  
  160.         Calendar calLast = Calendar.getInstance();  
  161.         calLast.set(year, 07);  
  162.         Date firstDate = getLastDayOfWeek(calLast.getTime());  
  163.   
  164.         Calendar firstDateCal = Calendar.getInstance();  
  165.         firstDateCal.setTime(firstDate);  
  166.   
  167.         Calendar c = new GregorianCalendar();  
  168.         c.set(Calendar.YEAR, year);  
  169.         c.set(Calendar.MONTH, Calendar.JANUARY);  
  170.         c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));  
  171.   
  172.         Calendar cal = (GregorianCalendar) c.clone();  
  173.         cal.add(Calendar.DATE, (week - 1) * 7);  
  174.         Date lastDate = getLastDayOfWeek(cal.getTime());  
  175.   
  176.         return lastDate;  
  177.     }  
  178.   
  179.     /** 
  180.      *获取当前日期的年、月、日 
  181.      */  
  182.     public void display() {  
  183.         Calendar cal = Calendar.getInstance();  
  184.         // 年  
  185.         int year = cal.get(cal.YEAR);  
  186.         // 月  
  187.         int month = cal.get(cal.MONTH) + 1;  
  188.         // 日  
  189.         int date = cal.get(cal.DATE);  
  190.         // 星期  
  191.         int today = cal.get(cal.DAY_OF_WEEK) - 1;  
  192.     }  
  193.       
  194.       
  195.     public static void  main(String args[]){  
  196.   
  197.         List<String> listWeekDate =  DateUtil.getWeekDays(2010,52);  
  198.           
  199.         for(String weeks :listWeekDate){  
  200.             System.out.println("weeks:"+weeks);  
  201.         }  
  202.     }  
  203. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值