java获取当天,前天,明天,本周,本月,本年的开始日期时间和结束日期时间


 1 package demoone;  2 
 3 import java.sql.Timestamp;  4 import java.text.ParseException;  5 import java.text.SimpleDateFormat;  6 import java.util.ArrayList;  7 import java.util.Calendar;  8 import java.util.Date;  9 import java.util.GregorianCalendar;  10 import java.util.List;  11 
 12 public class DateUtils {  13     //获取当天的开始时间
 14     public static java.util.Date getDayBegin() {  15         Calendar cal = new GregorianCalendar();  16         cal.set(Calendar.HOUR_OF_DAY, 0);  17         cal.set(Calendar.MINUTE, 0);  18         cal.set(Calendar.SECOND, 0);  19         cal.set(Calendar.MILLISECOND, 0);  20         return cal.getTime();  21  }  22     //获取当天的结束时间
 23     public static java.util.Date getDayEnd() {  24         Calendar cal = new GregorianCalendar();  25         cal.set(Calendar.HOUR_OF_DAY, 23);  26         cal.set(Calendar.MINUTE, 59);  27         cal.set(Calendar.SECOND, 59);  28         return cal.getTime();  29  }  30     //获取昨天的开始时间
 31     public static Date getBeginDayOfYesterday() {  32         Calendar cal = new GregorianCalendar();  33  cal.setTime(getDayBegin());  34         cal.add(Calendar.DAY_OF_MONTH, -1);  35         return cal.getTime();  36  }  37     //获取昨天的结束时间
 38     public static Date getEndDayOfYesterDay() {  39         Calendar cal = new GregorianCalendar();  40  cal.setTime(getDayEnd());  41         cal.add(Calendar.DAY_OF_MONTH, -1);  42         return cal.getTime();  43  }  44     //获取明天的开始时间
 45     public static Date getBeginDayOfTomorrow() {  46         Calendar cal = new GregorianCalendar();  47  cal.setTime(getDayBegin());  48         cal.add(Calendar.DAY_OF_MONTH, 1);  49 
 50         return cal.getTime();  51  }  52     //获取明天的结束时间
 53     public static Date getEndDayOfTomorrow() {  54         Calendar cal = new GregorianCalendar();  55  cal.setTime(getDayEnd());  56         cal.add(Calendar.DAY_OF_MONTH, 1);  57         return cal.getTime();  58  }  59     //获取本周的开始时间
 60     public static Date getBeginDayOfWeek() {  61         Date date = new Date();  62         if (date == null) {  63             return null;  64  }  65         Calendar cal = Calendar.getInstance();  66  cal.setTime(date);  67         int dayofweek = cal.get(Calendar.DAY_OF_WEEK);  68         if (dayofweek == 1) {  69             dayofweek += 7;  70  }  71         cal.add(Calendar.DATE, 2 - dayofweek);  72         return getDayStartTime(cal.getTime());  73  }  74     //获取本周的结束时间
 75     public static Date getEndDayOfWeek(){  76         Calendar cal = Calendar.getInstance();  77  cal.setTime(getBeginDayOfWeek());  78         cal.add(Calendar.DAY_OF_WEEK, 6);  79         Date weekEndSta = cal.getTime();  80         return getDayEndTime(weekEndSta);  81  }  82     //获取本月的开始时间
 83      public static Date getBeginDayOfMonth() {  84             Calendar calendar = Calendar.getInstance();  85             calendar.set(getNowYear(), getNowMonth() - 1, 1);  86             return getDayStartTime(calendar.getTime());  87  }  88     //获取本月的结束时间
 89      public static Date getEndDayOfMonth() {  90             Calendar calendar = Calendar.getInstance();  91             calendar.set(getNowYear(), getNowMonth() - 1, 1);  92             int day = calendar.getActualMaximum(5);  93             calendar.set(getNowYear(), getNowMonth() - 1, day);  94             return getDayEndTime(calendar.getTime());  95  }  96      //获取本年的开始时间
 97      public static java.util.Date getBeginDayOfYear() {  98             Calendar cal = Calendar.getInstance();  99  cal.set(Calendar.YEAR, getNowYear()); 100             // cal.set
101  cal.set(Calendar.MONTH, Calendar.JANUARY); 102             cal.set(Calendar.DATE, 1); 103 
104             return getDayStartTime(cal.getTime()); 105  } 106      //获取本年的结束时间
107      public static java.util.Date getEndDayOfYear() { 108             Calendar cal = Calendar.getInstance(); 109  cal.set(Calendar.YEAR, getNowYear()); 110  cal.set(Calendar.MONTH, Calendar.DECEMBER); 111             cal.set(Calendar.DATE, 31); 112             return getDayEndTime(cal.getTime()); 113  } 114     //获取某个日期的开始时间
115     public static Timestamp getDayStartTime(Date d) { 116         Calendar calendar = Calendar.getInstance(); 117         if(null != d) calendar.setTime(d); 118         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); 119         calendar.set(Calendar.MILLISECOND, 0); 120         return new Timestamp(calendar.getTimeInMillis()); 121  } 122     //获取某个日期的结束时间
123     public static Timestamp getDayEndTime(Date d) { 124         Calendar calendar = Calendar.getInstance(); 125         if(null != d) calendar.setTime(d); 126         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59); 127         calendar.set(Calendar.MILLISECOND, 999); 128         return new Timestamp(calendar.getTimeInMillis()); 129  } 130     //获取今年是哪一年
131      public static Integer getNowYear() { 132              Date date = new Date(); 133             GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); 134  gc.setTime(date); 135             return Integer.valueOf(gc.get(1)); 136  } 137      //获取本月是哪一月
138      public static int getNowMonth() { 139              Date date = new Date(); 140             GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); 141  gc.setTime(date); 142             return gc.get(2) + 1; 143  } 144      //两个日期相减得到的天数
145      public static int getDiffDays(Date beginDate, Date endDate) { 146 
147             if (beginDate == null || endDate == null) { 148                 throw new IllegalArgumentException("getDiffDays param is null!"); 149  } 150 
151             long diff = (endDate.getTime() - beginDate.getTime()) 152                     / (1000 * 60 * 60 * 24); 153 
154             int days = new Long(diff).intValue(); 155 
156             return days; 157  } 158     //两个日期相减得到的毫秒数
159      public static long dateDiff(Date beginDate, Date endDate) { 160             long date1ms = beginDate.getTime(); 161             long date2ms = endDate.getTime(); 162             return date2ms - date1ms; 163  } 164      //获取两个日期中的最大日期
165      public static Date max(Date beginDate, Date endDate) { 166             if (beginDate == null) { 167                 return endDate; 168  } 169             if (endDate == null) { 170                 return beginDate; 171  } 172             if (beginDate.after(endDate)) { 173                 return beginDate; 174  } 175             return endDate; 176  } 177      //获取两个日期中的最小日期
178      public static Date min(Date beginDate, Date endDate) { 179             if (beginDate == null) { 180                 return endDate; 181  } 182             if (endDate == null) { 183                 return beginDate; 184  } 185             if (beginDate.after(endDate)) { 186                 return endDate; 187  } 188             return beginDate; 189  } 190      //返回某月该季度的第一个月
191      public static Date getFirstSeasonDate(Date date) { 192              final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 }; 193             Calendar cal = Calendar.getInstance(); 194  cal.setTime(date); 195             int sean = SEASON[cal.get(Calendar.MONTH)]; 196             cal.set(Calendar.MONTH, sean * 3 - 3); 197             return cal.getTime(); 198  } 199      //返回某个日期下几天的日期
200      public static Date getNextDay(Date date, int i) { 201             Calendar cal = new GregorianCalendar(); 202  cal.setTime(date); 203             cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i); 204             return cal.getTime(); 205  } 206      //返回某个日期前几天的日期
207      public static Date getFrontDay(Date date, int i) { 208             Calendar cal = new GregorianCalendar(); 209  cal.setTime(date); 210             cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i); 211             return cal.getTime(); 212  } 213      //获取某年某月到某年某月按天的切片日期集合(间隔天数的日期集合)
214      public static List getTimeList(int beginYear, int beginMonth, int endYear, 215                 int endMonth, int k) { 216             List list = new ArrayList(); 217             if (beginYear == endYear) { 218                 for (int j = beginMonth; j <= endMonth; j++) { 219  list.add(getTimeList(beginYear, j, k)); 220 
221  } 222             } else { 223  { 224                     for (int j = beginMonth; j < 12; j++) { 225  list.add(getTimeList(beginYear, j, k)); 226  } 227 
228                     for (int i = beginYear + 1; i < endYear; i++) { 229                         for (int j = 0; j < 12; j++) { 230  list.add(getTimeList(i, j, k)); 231  } 232  } 233                     for (int j = 0; j <= endMonth; j++) { 234  list.add(getTimeList(endYear, j, k)); 235  } 236  } 237  } 238             return list; 239  } 240      //获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
241      public static List getTimeList(int beginYear, int beginMonth, int k) { 242             List list = new ArrayList(); 243             Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1); 244             int max = begincal.getActualMaximum(Calendar.DATE); 245             for (int i = 1; i < max; i = i + k) { 246  list.add(begincal.getTime()); 247  begincal.add(Calendar.DATE, k); 248  } 249             begincal = new GregorianCalendar(beginYear, beginMonth, max); 250  list.add(begincal.getTime()); 251             return list; 252  } 253 }

 

 1 //获取某年某月的第一天日期
 2  public static Date getStartMonthDate(int year, int month) {
 3         Calendar calendar = Calendar.getInstance();
 4         calendar.set(year, month - 1, 1);
 5         return calendar.getTime();
 6     }
 7 
 8 //获取某年某月的最后一天日期
 9    public static Date getEndMonthDate(int year, int month) {
10         Calendar calendar = Calendar.getInstance();
11         calendar.set(year, month - 1, 1);
12         int day = calendar.getActualMaximum(5);
13         calendar.set(year, month - 1, day);
14         return calendar.getTime();
15     }

 

DateUtils下载

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM