日期操作的工具类

  1 public class DateUtil {
  2
  3     private String year;
  4     private String month;
  5     private String day;
  6
  7     /**
  8      * 月份字符串最小值
  9      */
 10     public static final String MONTH_MIN = "01";
 11     /**
 12      * 月份字符串最大值
 13      */
 14     public static final String MONTH_MAX = "12";
 15
 16     /**
 17      * 天数字符串最小值
 18      */
 19     public static final String DAY_MIN = "01";
 20     /**
 21      * 天数字符串最大值
 22      */
 23     public static final String DAY_MAX = "31";
 24
 25     public DateUtil() {
 26         String dateTime = DateTime.getNowDateTime();
 27         this.year = dateTime.substring(0, 4);
 28         this.month = dateTime.substring(5, 7);
 29         this.day = dateTime.substring(8, 10);
 30     }
 31
 32     public int getCurYearMin() {
 33         return Integer.parseInt(this.year + MONTH_MIN + DAY_MIN);
 34     }
 35
 36     public int getCurYearMax() {
 37         return Integer.parseInt(this.year + MONTH_MAX + DAY_MAX);
 38     }
 39
 40     public int getCurMonthMin() {
 41         return Integer.parseInt(this.year + this.month + DAY_MIN);
 42     }
 43
 44     public int getCurMonthMax() {
 45         return Integer.parseInt(this.year + this.month + DAY_MAX);
 46     }
 47
 48     public int getCurDay() {
 49         return Integer.parseInt(this.year + this.month + this.day);
 50     }
 51
 52     /**
 53      * 获取本年到本月的所有月份
 54      * @return new String[]{"2010-01", "2010-02", "2010-03"}
 55      */
 56     public String[] months(){
 57         int m = Integer.parseInt(month);
 58         String res[] =new String[m];
 59         for(int i=1; i<=m; i++){
 60             if(i<10) {
 61                 res[i-1] = year + "-0" + i;
 62             } else {
 63                 res[i-1] = year + "-" + i;
 64             }
 65         }
 66         return res;
 67
 68     }
 69
 70     /**
 71      * 12个月
 72      * @param year
 73      * @return if null return 最近12个月, else return 年12个月
 74      */
 75     public String[] months(String year){
 76         if(null == year){
 77             return months12();
 78         }return months12(Integer.parseInt(year));
 79
 80     }
 81     public String[] months(String begin, String end){
 82         DateFormat df = new SimpleDateFormat("yyyy-MM");
 83         ArrayList l = new ArrayList();
 84         try {
 85             Date db = df.parse(begin);
 86             Calendar cb = new GregorianCalendar();
 87             cb.setTime(db);
 88             Date de = df.parse(end);
 89             Calendar ce = new GregorianCalendar();
 90             ce.setTime(de);
 91             l.add(df.format(cb.getTime()));
 92             while(cb.before(ce)){
 93                 cb.add(Calendar.MONTH, 1);
 94                 l.add(df.format(cb.getTime()));
 95             }
 96         } catch (ParseException e) {
 97             e.printStackTrace();
 98         }
 99         int m = l.size();
100         String res[] =new String[m];
101         for(int i=0; i<m; i++){
102             res[i] = (String) l.get(i);
103         }
104         return res;
105
106     }
107     /**
108      * 获取当前月的开始时间
109      * @return 2011-01-01 00:00:00
110      */
111     public String getCurrentMonthBegin(){
112         return date(null, 0, 0, 0, 0, 0, 0);
113     }
114
115     /**
116      * 获取当前月的结束时间
117      * @return 2011-01-31 23:59:59
118      */
119     public String getCurrentMonthEnd(){
120         return date(null, 0, 1, 0, 0, 0, -1);
121     }
122
123     /**
124      * 获取前一个月的开始时间
125      * @return 2010-12-01 00:00:00
126      */
127     public String getPreviousMonthBegin(){
128         return date(null, 0, -1, 0, 0, 0, 0);
129     }
130
131     /**
132      * 获取前一个月的结束时间
133      * @return 2010-12-31 23:59:59
134      */
135     public String getPreviousMonthEnd(){
136         return date(null, 0, 0, 0, 0, 0, -1);
137     }
138
139     /**
140      * 获取当前季度的开始时间
141      * @return 2011-01-01 00:00:00
142      */
143     public String getCurrentSeasonBegin() {
144         return getSeason(0);
145     }
146
147     /**
148      * 获取当前季度的结束时间
149      * @return 2011-03-31 23:59:59
150      */
151     public String getCurrentSeasonEnd() {
152         return getSeason(1);
153     }
154
155     /**
156      * 当前季度的时间
157      * @param m 0:当前季度的开始时间 1:当前季度的结束时间
158      * @return
159      */
160     private String getSeason(int m) {
161         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
162         Date d = new Date();
163         Calendar c = Calendar.getInstance();
164         c.setTime(d);
165         int month = ((c.get(Calendar.MONTH) / 3) + m) * 3;
166         c.set(c.get(Calendar.YEAR), month, 1, 0, 0, -m);
167         return df.format(c.getTime());
168     }
169
170     /**
171      * 获取当前年的开始时间
172      * @return 2011-01-01 00:00:00
173      */
174     public String getCurrentYearBegin(){
175         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
176         Date date = new Date();
177         Calendar c = Calendar.getInstance();
178         c.setTime(date);
179         c.set(c.get(Calendar.YEAR), 0, 1, 0, 0, 0);
180         return df.format(c.getTime());
181     }
182
183     /**
184      * 获取当前时间
185      * @return 2011-01-05 13:49:59
186      */
187     public String getCurrentYearEnd(){
188         return DateTime.getNowDateTime();
189     }
190
191     /**
192      * 昨天的开始时间
193      * @return 2010-12-01 00:00:00
194      */
195     public String getYesterdayBegin(){
196         return dateday(null, 0, 0, -1, 0, 0, 0);
197     }
198
199     /**
200      * 昨天的结束时间
201      * @return 2010-12-31 23:59:59
202      */
203     public String getYesterdayEnd(){
204         return dateday(null, 0, 0, 0, 0, 0, -1);
205     }
206
207     /**
208      * 今天的开始时间
209      * @return 2010-12-01 00:00:00
210      */
211     public String getTodayBegin(){
212         return dateday(null, 0, 0, 0, 0, 0, 0);
213     }
214
215     /**
216      * 今天的结束时间
217      * @return 2010-12-31 23:59:59
218      */
219     public String getTodayEnd(){
220         return dateday(null, 0, 0, 1, 0, 0, -1);
221     }
222
223     /**
224      * 本周的开始时间
225      * @return 2010-12-01 00:00:00
226      */
227     public String getWeekBegin(){
228         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
229         Date date = new Date();
230         Calendar c = Calendar.getInstance();
231         c.setTime(date);
232         c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
233         c.add(Calendar.DAY_OF_MONTH, 1-c.get(Calendar.DAY_OF_WEEK));
234         return df.format(c.getTime());
235
236     }
237
238     /**
239      * 本周结束时间
240      * @return 2010-12-31 23:59:59
241      */
242     public String getWeekEnd(){
243         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
244         Date date = new Date();
245         Calendar c = Calendar.getInstance();
246         c.setTime(date);
247         c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
248         c.add(Calendar.DAY_OF_MONTH, 1-c.get(Calendar.DAY_OF_WEEK));
249         c.add(Calendar.WEEK_OF_MONTH, 1);
250         c.add(Calendar.SECOND, -1);
251         return df.format(c.getTime());
252         }
253
254     /**
255      * 某个时间的变化
256      * @param date 日期时间
257      * @param year 年
258      * @param month 月
259      * @param day 天
260      * @param hour 时
261      * @param minute 分
262      * @param second 秒
263      * @return 2011-01-01 00:00:00 or 2011-01-31 23:59:59
264      */
265     public String date(Date date, int year, int month, int day, int hour, int minute, int second){
266 //        DateFormat df = new SimpleDateFormat("yyyyMMdd");
267         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
268         if(date == null){
269             date = new Date();
270         }
271         Calendar c = Calendar.getInstance();
272         c.setTime(date);
273         c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0);
274         c.add(Calendar.YEAR, year);
275         c.add(Calendar.MONTH, month);
276         c.add(Calendar.DAY_OF_MONTH, day);
277         c.add(Calendar.HOUR_OF_DAY, hour);
278         c.add(Calendar.MINUTE, minute);
279         c.add(Calendar.SECOND, second);
280         return df.format(c.getTime());
281     }
282     /**
283      * 某个时间的变化
284      * @param date 日期时间
285      * @param year 年
286      * @param month 月
287      * @param day 天
288      * @param hour 时
289      * @param minute 分
290      * @param second 秒
291      * @return 2011-01-01 00:00:00 or 2011-01-31 23:59:59
292      */
293     private String dateday(Date date, int year, int month, int day, int hour, int minute, int second){
294 //        DateFormat df = new SimpleDateFormat("yyyyMMdd");
295         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
296         if(date == null){
297             date = new Date();
298         }
299         Calendar c = Calendar.getInstance();
300         c.setTime(date);
301         c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
302         c.add(Calendar.YEAR, year);
303         c.add(Calendar.MONTH, month);
304         c.add(Calendar.DAY_OF_MONTH, day);
305         c.add(Calendar.HOUR_OF_DAY, hour);
306         c.add(Calendar.MINUTE, minute);
307         c.add(Calendar.SECOND, second);
308         return df.format(c.getTime());
309     }
310
311     /**
312      * 两个日期之间的全部日期数值数组
313      * @param begin
314      * @param end
315      * @return{2010-10-10, 2010-10-11, 2010-10-12, ,...,2011-01-11}
316      */
317     public String[] dates(String begin ,String end){
318         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
319         String[] arr = new String[]{""};
320         try {
321             Date db = df.parse(begin);
322             Calendar cb = Calendar.getInstance();
323             cb.setTime(db);
324             Date de = df.parse(end);
325             Calendar ce = Calendar.getInstance();
326             ce.setTime(de);
327             long lday = (ce.getTimeInMillis() - cb.getTimeInMillis()) / (1000 * 60 * 60 * 24);
328             int lg = Long.valueOf(lday).intValue();
329             arr = new String[lg + 1];
330             arr[0] = df.format(cb.getTime());
331             for(int i=0; i < lg; i++){
332                 cb.add(Calendar.DAY_OF_YEAR, 1);
333                 arr[i + 1] = df.format(cb.getTime());
334             }
335         } catch (ParseException e) {
336             e.printStackTrace();
337         }
338         return arr;
339
340     }
341
342     /**
343      * 最近12个月
344      * @param begin
345      * @param end
346      * @return {2010-02, 2010-03, 2010-04,..,2011-01}
347      */
348     public String[] months12(){
349         DateFormat df = new SimpleDateFormat("yyyy-MM");
350         String[] arr = new String[12];
351         Date d = new Date();
352         Calendar cb = Calendar.getInstance();
353         cb.setTime(d);
354         cb.add(Calendar.MONTH, -12);
355         for(int i=0; i < 12; i++){
356             cb.add(Calendar.MONTH, 1);
357             arr[i] = df.format(cb.getTime());
358         }
359         return arr;
360     }
361     /**
362      * 某年的12个月
363      * @param begin
364      * @param end
365      * @return {2010-01, 2010-02, 2010-03, 2010-04,..,2010-12}
366      */
367     public String[] months12(int year){
368         DateFormat df = new SimpleDateFormat("yyyy-MM");
369         String[] arr = new String[12];
370         Calendar cb = Calendar.getInstance();
371         if(year == cb.get(Calendar.YEAR)){
372             return months();
373         }
374         cb.set(year, -1, 1);
375         for(int i=0; i < 12; i++){
376             cb.add(Calendar.MONTH, 1);
377             arr[i] = df.format(cb.getTime());
378         }
379         return arr;
380     }
381
382     /**
383      * 从当前年到2008年
384      * @param begin
385      * @param end
386      * @return {2011, 2010, 2009, 2008}
387      */
388     public String[] years(){
389         DateFormat df = new SimpleDateFormat("yyyy");
390
391         Date d = new Date();
392         Calendar cb = Calendar.getInstance();
393         int cy = cb.get(Calendar.YEAR);
394         cb.set(Calendar.YEAR, 2007);
395         cy = cy - cb.get(Calendar.YEAR);
396         cb.setTime(d);
397         cb.add(Calendar.YEAR, 1);
398         String[] arr = new String[cy];
399         for(int i=0; i < cy; i++){
400             cb.add(Calendar.YEAR, -1);
401             arr[i] = df.format(cb.getTime());
402         }
403         return arr;
404     }
405     /**
406      * 获得当前的时间的前第十二个月
407      * @return
408      */
409     public String get12thMonth(){
410         DateFormat df = new SimpleDateFormat("yyyy-MM");
411         Date d = new Date();
412         GregorianCalendar cb = new GregorianCalendar();
413         cb.setTime(d);
414         cb.add(Calendar.MONTH, -11);
415         return df.format(cb.getTime());
416     }
417
418     /**
419      * 获得当前年月
420      * @return
421      */
422     public String getCurrentMonth(){
423         DateFormat df = new SimpleDateFormat("yyyy-MM");
424         GregorianCalendar cd = new GregorianCalendar();
425         return df.format(cd.getTime());
426     }
427428}

public class DateUtil {
private String year;private String month;private String day;
/** * 月份字符串最小值 */public static final String MONTH_MIN = "01";/** * 月份字符串最大值 */public static final String MONTH_MAX = "12";
/** * 天数字符串最小值 */public static final String DAY_MIN = "01";/** * 天数字符串最大值 */public static final String DAY_MAX = "31";
public DateUtil() {String dateTime = DateTime.getNowDateTime();this.year = dateTime.substring(0, 4);this.month = dateTime.substring(5, 7);this.day = dateTime.substring(8, 10);}
public int getCurYearMin() {return Integer.parseInt(this.year + MONTH_MIN + DAY_MIN);}
public int getCurYearMax() {return Integer.parseInt(this.year + MONTH_MAX + DAY_MAX);}
public int getCurMonthMin() {return Integer.parseInt(this.year + this.month + DAY_MIN);}
public int getCurMonthMax() {return Integer.parseInt(this.year + this.month + DAY_MAX);}
public int getCurDay() {return Integer.parseInt(this.year + this.month + this.day);}
/** * 获取本年到本月的所有月份 * @return new String[]{"2010-01", "2010-02", "2010-03"} */public String[] months(){int m = Integer.parseInt(month);String res[] =new String[m];for(int i=1; i<=m; i++){if(i<10) {res[i-1] = year + "-0" + i;} else {res[i-1] = year + "-" + i;}}return res;
}/** * 12个月 * @param year * @return if null return 最近12个月, else return 年12个月 */public String[] months(String year){if(null == year){return months12();}return months12(Integer.parseInt(year));}public String[] months(String begin, String end){DateFormat df = new SimpleDateFormat("yyyy-MM");ArrayList l = new ArrayList();try {Date db = df.parse(begin);Calendar cb = new GregorianCalendar();cb.setTime(db);Date de = df.parse(end);Calendar ce = new GregorianCalendar();ce.setTime(de);l.add(df.format(cb.getTime()));while(cb.before(ce)){cb.add(Calendar.MONTH, 1);l.add(df.format(cb.getTime()));}} catch (ParseException e) {e.printStackTrace();}int m = l.size();String res[] =new String[m];for(int i=0; i<m; i++){res[i] = (String) l.get(i); }return res;}/** * 获取当前月的开始时间 * @return 2011-01-01 00:00:00 */public String getCurrentMonthBegin(){return date(null, 0, 0, 0, 0, 0, 0);}
/** * 获取当前月的结束时间 * @return 2011-01-31 23:59:59 */public String getCurrentMonthEnd(){return date(null, 0, 1, 0, 0, 0, -1);}/** * 获取前一个月的开始时间 * @return 2010-12-01 00:00:00 */public String getPreviousMonthBegin(){return date(null, 0, -1, 0, 0, 0, 0);}
/** * 获取前一个月的结束时间 * @return 2010-12-31 23:59:59 */public String getPreviousMonthEnd(){return date(null, 0, 0, 0, 0, 0, -1);}
/** * 获取当前季度的开始时间 * @return 2011-01-01 00:00:00 */public String getCurrentSeasonBegin() {return getSeason(0);}
/** * 获取当前季度的结束时间 * @return 2011-03-31 23:59:59 */public String getCurrentSeasonEnd() {return getSeason(1);}
/** * 当前季度的时间 * @param m 0:当前季度的开始时间 1:当前季度的结束时间 * @return */private String getSeason(int m) {DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date d = new Date();Calendar c = Calendar.getInstance();c.setTime(d);int month = ((c.get(Calendar.MONTH) / 3) + m) * 3;c.set(c.get(Calendar.YEAR), month, 1, 0, 0, -m);return df.format(c.getTime());}/** * 获取当前年的开始时间 * @return 2011-01-01 00:00:00 */public String getCurrentYearBegin(){DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();Calendar c = Calendar.getInstance();c.setTime(date);c.set(c.get(Calendar.YEAR), 0, 1, 0, 0, 0);return df.format(c.getTime());}
/** * 获取当前时间 * @return 2011-01-05 13:49:59 */public String getCurrentYearEnd(){return DateTime.getNowDateTime();}
/** * 昨天的开始时间 * @return 2010-12-01 00:00:00 */public String getYesterdayBegin(){return dateday(null, 0, 0, -1, 0, 0, 0);}
/** * 昨天的结束时间 * @return 2010-12-31 23:59:59 */public String getYesterdayEnd(){return dateday(null, 0, 0, 0, 0, 0, -1);}
/** * 今天的开始时间 * @return 2010-12-01 00:00:00 */public String getTodayBegin(){return dateday(null, 0, 0, 0, 0, 0, 0);}
/** * 今天的结束时间 * @return 2010-12-31 23:59:59 */public String getTodayEnd(){return dateday(null, 0, 0, 1, 0, 0, -1);}
/** * 本周的开始时间 * @return 2010-12-01 00:00:00 */public String getWeekBegin(){DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();Calendar c = Calendar.getInstance();c.setTime(date);c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);c.add(Calendar.DAY_OF_MONTH, 1-c.get(Calendar.DAY_OF_WEEK));return df.format(c.getTime());}
/** * 本周结束时间 * @return 2010-12-31 23:59:59 */public String getWeekEnd(){DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();Calendar c = Calendar.getInstance();c.setTime(date);c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);c.add(Calendar.DAY_OF_MONTH, 1-c.get(Calendar.DAY_OF_WEEK));c.add(Calendar.WEEK_OF_MONTH, 1);c.add(Calendar.SECOND, -1);return df.format(c.getTime());}
/** * 某个时间的变化 * @param date 日期时间 * @param year 年 * @param month 月 * @param day 天 * @param hour 时 * @param minute 分 * @param second 秒 * @return 2011-01-01 00:00:00 or 2011-01-31 23:59:59 */public String date(Date date, int year, int month, int day, int hour, int minute, int second){//DateFormat df = new SimpleDateFormat("yyyyMMdd");DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");if(date == null){date = new Date();}Calendar c = Calendar.getInstance();c.setTime(date);c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0);c.add(Calendar.YEAR, year);c.add(Calendar.MONTH, month);c.add(Calendar.DAY_OF_MONTH, day);c.add(Calendar.HOUR_OF_DAY, hour);c.add(Calendar.MINUTE, minute);c.add(Calendar.SECOND, second);return df.format(c.getTime());}/** * 某个时间的变化 * @param date 日期时间 * @param year 年 * @param month 月 * @param day 天 * @param hour 时 * @param minute 分 * @param second 秒 * @return 2011-01-01 00:00:00 or 2011-01-31 23:59:59 */private String dateday(Date date, int year, int month, int day, int hour, int minute, int second){//DateFormat df = new SimpleDateFormat("yyyyMMdd");DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");if(date == null){date = new Date();}Calendar c = Calendar.getInstance();c.setTime(date);c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);c.add(Calendar.YEAR, year);c.add(Calendar.MONTH, month);c.add(Calendar.DAY_OF_MONTH, day);c.add(Calendar.HOUR_OF_DAY, hour);c.add(Calendar.MINUTE, minute);c.add(Calendar.SECOND, second);return df.format(c.getTime());}/** * 两个日期之间的全部日期数值数组 * @param begin * @param end * @return{2010-10-10, 2010-10-11, 2010-10-12, ,...,2011-01-11} */public String[] dates(String begin ,String end){DateFormat df = new SimpleDateFormat("yyyy-MM-dd");String[] arr = new String[]{""};try {Date db = df.parse(begin);Calendar cb = Calendar.getInstance();cb.setTime(db);Date de = df.parse(end);Calendar ce = Calendar.getInstance();ce.setTime(de);long lday = (ce.getTimeInMillis() - cb.getTimeInMillis()) / (1000 * 60 * 60 * 24);int lg = Long.valueOf(lday).intValue();arr = new String[lg + 1];arr[0] = df.format(cb.getTime());for(int i=0; i < lg; i++){cb.add(Calendar.DAY_OF_YEAR, 1);arr[i + 1] = df.format(cb.getTime());}} catch (ParseException e) {e.printStackTrace();}return arr;}/** * 最近12个月 * @param begin * @param end * @return {2010-02, 2010-03, 2010-04,..,2011-01} */public String[] months12(){DateFormat df = new SimpleDateFormat("yyyy-MM");String[] arr = new String[12];Date d = new Date();Calendar cb = Calendar.getInstance();cb.setTime(d);cb.add(Calendar.MONTH, -12);for(int i=0; i < 12; i++){cb.add(Calendar.MONTH, 1);arr[i] = df.format(cb.getTime());}return arr;}/** * 某年的12个月 * @param begin * @param end * @return {2010-01, 2010-02, 2010-03, 2010-04,..,2010-12} */public String[] months12(int year){DateFormat df = new SimpleDateFormat("yyyy-MM");String[] arr = new String[12];Calendar cb = Calendar.getInstance();if(year == cb.get(Calendar.YEAR)){return months();}cb.set(year, -1, 1);for(int i=0; i < 12; i++){cb.add(Calendar.MONTH, 1);arr[i] = df.format(cb.getTime());}return arr;}/** * 从当前年到2008年 * @param begin * @param end * @return {2011, 2010, 2009, 2008} */public String[] years(){DateFormat df = new SimpleDateFormat("yyyy");Date d = new Date();Calendar cb = Calendar.getInstance();int cy = cb.get(Calendar.YEAR);cb.set(Calendar.YEAR, 2007);cy = cy - cb.get(Calendar.YEAR);cb.setTime(d);cb.add(Calendar.YEAR, 1);String[] arr = new String[cy];for(int i=0; i < cy; i++){cb.add(Calendar.YEAR, -1);arr[i] = df.format(cb.getTime());}return arr;}/** * 获得当前的时间的前第十二个月 * @return */public String get12thMonth(){     DateFormat df = new SimpleDateFormat("yyyy-MM");Date d = new Date();GregorianCalendar cb = new GregorianCalendar();cb.setTime(d);cb.add(Calendar.MONTH, -11);return df.format(cb.getTime());}  /** * 获得当前年月 * @return */     public String getCurrentMonth(){     DateFormat df = new SimpleDateFormat("yyyy-MM");GregorianCalendar cd = new GregorianCalendar();return df.format(cd.getTime());}
/** * @param args */public static void main(String[] args) {DateUtil du = new DateUtil();if(10 > 2){System.out.println(du.getCurrentYearEnd());System.out.println(du.getWeekBegin());System.out.println(du.getWeekEnd());}else{System.out.println(du.getCurYearMin() + "~" + du.getCurYearMax());
System.out.println(du.getCurMonthMin() + "~" + du.getCurMonthMax());
System.out.println(du.getCurDay());

System.out.println(du.getPreviousMonthBegin());System.out.println(du.getPreviousMonthEnd());
System.out.println(du.getCurrentSeasonBegin());System.out.println(du.getCurrentSeasonEnd());int m = 0;int t = -3;Date d = new Date();Calendar c = Calendar.getInstance();c.setTime(d);int month = (((c.get(Calendar.MONTH) + t) / 3) + m) * 3;System.out.println(month);

String[] arr = du.dates("2010-10-10", "2011-01-11");int lg = arr.length;for(int i=0; i < lg; i++){System.out.println(arr[i]);}arr = du.months12();lg = arr.length;for(int i=0; i < lg; i++){System.out.println(arr[i]);}
arr = du.years();lg = arr.length;for(int i=0; i < lg; i++){System.out.println(arr[i]);}arr = du.months12(2011);lg = arr.length;for(int i=0; i < lg; i++){System.out.println(arr[i]);}System.out.println(" -- - -");arr = du.months("2010-10-10", "2011-01-11");lg = arr.length;for(int i=0; i < lg; i++){System.out.println(arr[i]);}

System.out.println(du.getTodayBegin());System.out.println(du.getTodayEnd());System.out.println(" -- - ");System.out.println(du.getYesterdayBegin());System.out.println(du.getYesterdayEnd());System.out.println(" -- - ");System.out.println(du.getWeekBegin());System.out.println(du.getWeekEnd());
}}}

时间: 2024-08-29 14:58:31

日期操作的工具类的相关文章

c语言中字符串操作的工具类

 1.编写头文件 #define _CRT_SECURE_NO_WARNINGS //#pragmawarning(disable:4996) #include <stdio.h> #include <stdlib.h> #include <string.h> struct CString { char *p;        //保存字符串首地址 int reallength; //实际长度 }; typedef struct CString mystring;//

poi操作Excel工具类

在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完成的功能是:读取Excel.写入Excel.合并Excel的功能.

List操作的工具类

1 /** 2 * <p>list操作的工具类</p> 3 */ 4 public class ListUtil { 5 /** 6 * 过滤掉list里面才重复项 7 * 8 * @param list 9 * @return List 10 */ 11 public static List<String> filterRepeat(List<String> list){ 12 int length = list.size(); 13 for(int i

Java 借助poi操作Wold工具类

? Apache封装的POI组件对Excel,Wold的操作已经非常的丰富了,在项目上也会经常用到一些POI的基本操作 这里就简单的阐述POI操作Wold的基本工具类,代码还是有点粗造的,但是不影响使用. 这个类包含了一些对文本进行换行,加粗,倾斜,字体颜色,大小,首行缩进,添加边框等方法.分享给大家学习下: Apache POI的组件: ApachePOI包含用于处理MS-Office的所有OLE2复合文档的类和方法.该API的组件列表如下 - POIFS(不良混淆实现文件系统) - 此组件是

操作集合工具类:Collections

Collections是常用的操作Set.List.Map的工具类.提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合对象设置为不可变.对集合对象实现同步控制等方法. reverse 反转: /** * Reverses the order of the elements in the specified list.<p> * * This method runs in linear time. * * @param list the list whose elements a

自己封装的poi操作Excel工具类

该工具类主要完成的功能是:读取Excel.汇总Excel的功能.在读取时,可以设定开始和结束读取的位置.设定是否读取多个sheet.设定读取那个或者那些sheet等.在汇总时,如设定是否覆盖目标文件.设定是否比较检查重复内容.设定检查重复的列索引等功能. package com.tgb.ccl.excel.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; impo

java与javascript对cookie操作的工具类

Java对cookie的操作 package cn.utils; import java.util.HashMap; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 操作cookie的工具类(默认cookie的有效路径为"/")

转换日期格式的工具类

写一个工具类,用来转换固定的日期格式: import java.text.SimpleDateFormat; import java.util.Date; public class DateFormat { public static String convert(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = sdf.format(date); retur

(4)文本挖掘(一)——准备文本读写及对Map操作的工具类

文本挖掘是一个对具有丰富语义的文本进行分析,从而理解其所包含的内容和意义的过程.文本挖掘包含分词.文本表示.文本特征选择.文本分类.文本聚类.文档自动摘要等方面的内容.文本挖掘的具体流程图可下图所示: 我的项目是以复旦大学中文语料库和路透社英文语料库为数据集的,都是有类别的两层目录文本集. 不管你要做什么,你首先都要先读取文本,为了方便后面的操作,我写了几个工具类,这里先将文本读取Reader类.文本写入Writer类和对Map的各种操作MapUtil类. Reader import java.