码迷,mamicode.com
首页 > 编程语言 > 详细

CalendarUtil.java

时间:2014-07-20 10:23:27      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   color   2014   io   

  1 package com.honghailt.dataextracter.utils;
  2 
  3 import java.text.ParseException;
  4 import java.text.SimpleDateFormat;
  5 import java.util.ArrayList;
  6 import java.util.Calendar;
  7 import java.util.Date;
  8 import java.util.List;
  9 
 10 public  class CalendarUtil {
 11     
 12     //下月第一天
 13     public static Date getNextMonth(Date beginTime) {
 14         Calendar c = Calendar.getInstance();
 15         c.setTime(beginTime);
 16         c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
 17         c.set(Calendar.DAY_OF_MONTH, 1);
 18         return c.getTime();
 19     }
 20 
 21     //当天几号
 22     public static int getMonthOfDate(Date date) {
 23         Calendar c = Calendar.getInstance();
 24         c.setTime(date);
 25         return c.get(Calendar.DAY_OF_MONTH);
 26     }
 27     //当月有多少天
 28     public static int getLastDayOfMonth(Date date) {
 29         Calendar c = Calendar.getInstance();
 30         c.setTime(date);
 31         int month = c.getActualMaximum(Calendar.DATE);
 32         return month;
 33     }
 34    
 35     //当前月 -1
 36     public static int getMonth(Date date) {
 37         Calendar c = Calendar.getInstance();
 38         c.setTime(date);
 39         int month = c.get(Calendar.MONTH);
 40         return month;
 41     }
 42   
 43     //当月第一天
 44       public static Date getFirstDayOfMonth(Date beginTime){
 45            Calendar c = Calendar.getInstance();
 46            c.setTime(beginTime);
 47            c.set(Calendar.DAY_OF_MONTH, 1);
 48            return c.getTime();
 49       }
 50   
 51     //获得当周第几天  i=0表示星期一
 52     public static String getNowWeekBegin(Date date,int i) {
 53         Calendar cd = Calendar.getInstance();
 54         cd.setTime(date);
 55         cd.set(Calendar.DAY_OF_WEEK, Calendar.MONTH +i);
 56         if(i==6){
 57          cd.set(Calendar.DAY_OF_WEEK, Calendar.MONTH+5);
 58          cd.add(cd.DATE, 1); 
 59         }
 60         Date  monday =cd.getTime();
 61         SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
 62         String preMonday = df.format(monday);
 63         return preMonday ;
 64         } 
 65     
 66     //获得当周第几天,返回日期形。i=0表示星期一
 67     public static Date getNowWeekBeginDate(Date date,int i) {
 68         Calendar cd = Calendar.getInstance();
 69         cd.setTime(date);
 70         cd.set(Calendar.DAY_OF_WEEK, Calendar.MONTH +i);
 71         if(i==6){
 72          cd.set(Calendar.DAY_OF_WEEK, Calendar.MONTH+5);
 73          cd.add(cd.DATE, 1); 
 74         }
 75         Date  monday =cd.getTime();
 76         return monday ;
 77         } 
 78     //获得本周日期
 79     public static List<String> getDaysOfWeek(Date date){
 80         List<String> days = new ArrayList<String>();
 81         for (int i = 0; i < 7; i++) {
 82             days.add(getNowWeekBegin(date,i));
 83         }
 84         return days;
 85     }
 86     //获得本周日期
 87     public static List<Date> getDaysOfWeekDate(Date date){
 88         List<Date> days = new ArrayList<Date>();
 89         for (int i = 0; i < 7; i++) {
 90             days.add(getNowWeekBeginDate(date,i));
 91         }
 92         return days;
 93     }
 94     
 95     /**
 96      * 获得当月第几周
 97      * @param date
 98      * @param week
 99      * @return
100      */
101     public static List<String> getWeeksOfMonth(Date date,int week){
102         int day =7*week+1;
103         Calendar c = Calendar.getInstance();
104         c.setTime(date);
105         c.set(Calendar.DAY_OF_MONTH, day);
106         return getDaysOfWeek(c.getTime());
107     }
108   
109     //本月最后一天
110     public static Date getDateOfLastDayOfMonth(Date date){
111         Calendar c = Calendar.getInstance();
112         c.setTime(date);
113         c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DATE));
114         return c.getTime();
115     }
116     //获得本月第几天,0表示第一天
117    public static Date getDayOfMonth(Date date,int i){
118        Calendar c = Calendar.getInstance();
119            c.setTime(date);
120            c.set(Calendar.DAY_OF_MONTH, i+1);
121        return c.getTime();
122    }
123     //本月共几周
124     public static int getWeekyofMonth(Date date){
125         Date d1 = getFirstDayOfMonth(date);//第一天
126         Date d2 = getDateOfLastDayOfMonth(date);//最后一天
127         Date mon1 =getNowWeekBeginDate(d1,0);//第一天所在周星期一
128         Date mon2 =getNowWeekBeginDate(d2,0);//最后一天所在周星期一
129         return (int) ((mon2.getTime()-mon1.getTime())/3600/24/1000)/7;
130     }
131    public static List<String> getWeekName(){
132        List<String> list = new ArrayList<String>();
133        list.add("周一");
134        list.add("周二");
135        list.add("周三");
136        list.add("周四");
137        list.add("周五");
138        list.add("周六");
139        list.add("周日");
140        return list;
141        
142    }
143    /**
144     * 首先获得 定一个时间,就2014-07-14为单周
145     * 然后获得传过来日期的星期一与这个时间除以7.
146     * 然后结果除以2,如果余数是0,这是单周,否则是双周
147     * @param date
148     * @return
149     */
150  public static String getDanShuangWeek(Date date){
151     Date beginDate =StringToDate("2014-07-14");
152     Date nowDate =getNowWeekBeginDate(date,0); 
153     int temp = (int) ((nowDate.getTime() -beginDate.getTime())/3600/24/1000);
154     int danshuang=temp%2;
155     String str=null;
156     if(danshuang==0){
157         str="单周";
158     }else{
159         str="双周";
160     }
161      return str;
162  }
163  
164  public static int getDanShuangWeekInt(Date date){
165         Date beginDate =StringToDate("2014-07-14");
166         
167         Date nowDate =getNowWeekBeginDate(date,0); 
168         int temp = (int) ((nowDate.getTime() -beginDate.getTime())/3600/24/1000);
169         int danshuang=temp%2;
170          return danshuang;
171      }
172  
173  public static Date StringToDate(String strDate){
174      SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
175      Date date =null;
176      try {
177           date = df.parse(strDate);
178     } catch (ParseException e) {
179         // TODO Auto-generated catch block
180         e.printStackTrace();
181     }
182      return date;
183  }
184  
185  public static Date DateToDate(Date date){
186      SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
187      String strDate= df.format(date);
188      return StringToDate(strDate);
189  }
190  
191  public static void main(String[] args) {
192 //    System.out.println(getDanShuangWeek(StringToDate("2014-08-02"))); ;
193     // System.out.println(getDayOfMonth(new Date(),10));
194      System.out.println(DateToDate(new Date()));
195 }
196 }

CalendarUtil.java,布布扣,bubuko.com

CalendarUtil.java

标签:style   blog   java   color   2014   io   

原文地址:http://www.cnblogs.com/w8104254/p/3855858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!