日期工具类,提供对日期的格式化和转换方法、获取区间日期、指定日期、每月最后一天等。
源码如下:(点击下载 -、 )
1 import java.text.DateFormat; 2 import java.text.ParseException; 3 import java.text.SimpleDateFormat; 4 import java.util.ArrayList; 5 import java.util.Calendar; 6 import java.util.Date; 7 import java.util.GregorianCalendar; 8 import java.util.List; 9 import org.apache.commons.lang.StringUtils; 10 11 /** 12 * 日期工具类 13 * 14 */ 15 public class DateUtil { 16 17 // 默认日期格式 18 public static final String DATE_DEFAULT_FORMAT = "yyyy-MM-dd"; 19 20 // 默认时间格式 21 public static final String DATETIME_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss"; 22 23 public static final String TIME_DEFAULT_FORMAT = "HH:mm:ss"; 24 25 // 日期格式化 26 private static DateFormat dateFormat = null; 27 28 // 时间格式化 29 private static DateFormat dateTimeFormat = null; 30 31 private static DateFormat timeFormat = null; 32 33 private static Calendar gregorianCalendar = null; 34 35 static { 36 dateFormat = new SimpleDateFormat(DATE_DEFAULT_FORMAT); 37 dateTimeFormat = new SimpleDateFormat(DATETIME_DEFAULT_FORMAT); 38 timeFormat = new SimpleDateFormat(TIME_DEFAULT_FORMAT); 39 gregorianCalendar = new GregorianCalendar(); 40 } 41 42 /** 43 * 日期格式化yyyy-MM-dd 44 * 45 * @param date 46 * @return 47 */ 48 public static Date formatDate(String date, String format) { 49 try { 50 return new SimpleDateFormat(format).parse(date); 51 } catch (ParseException e) { 52 e.printStackTrace(); 53 } 54 return null; 55 } 56 57 /** 58 * 日期格式化yyyy-MM-dd 59 * 60 * @param date 61 * @return 62 */ 63 public static String getDateFormat(Date date) { 64 return dateFormat.format(date); 65 } 66 67 /** 68 * 日期格式化yyyy-MM-dd HH:mm:ss 69 * 70 * @param date 71 * @return 72 */ 73 public static String getDateTimeFormat(Date date) { 74 return dateTimeFormat.format(date); 75 } 76 77 /** 78 * 时间格式化 79 * 80 * @param date 81 * @return HH:mm:ss 82 */ 83 public static String getTimeFormat(Date date) { 84 return timeFormat.format(date); 85 } 86 87 /** 88 * 日期格式化 89 * 90 * @param date 91 * @param 格式类型 92 * @return 93 */ 94 public static String getDateFormat(Date date, String formatStr) { 95 if (StringUtils.isNotBlank(formatStr)) { 96 return new SimpleDateFormat(formatStr).format(date); 97 } 98 return null; 99 }100 101 /**102 * 日期格式化103 * 104 * @param date105 * @return106 */107 public static Date getDateFormat(String date) {108 try {109 return dateFormat.parse(date);110 } catch (ParseException e) {111 e.printStackTrace();112 }113 return null;114 }115 116 /**117 * 时间格式化118 * 119 * @param date120 * @return121 */122 public static Date getDateTimeFormat(String date) {123 try {124 return dateTimeFormat.parse(date);125 } catch (ParseException e) {126 e.printStackTrace();127 }128 return null;129 }130 131 /**132 * 获取当前日期(yyyy-MM-dd)133 * 134 * @param date135 * @return136 */137 public static Date getNowDate() {138 return DateUtil.getDateFormat(dateFormat.format(new Date()));139 }140 141 /**142 * 获取当前日期星期一日期143 * 144 * @return date145 */146 public static Date getFirstDayOfWeek() {147 gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);148 gregorianCalendar.setTime(new Date());149 gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek()); // Monday150 return gregorianCalendar.getTime();151 }152 153 /**154 * 获取当前日期星期日日期155 * 156 * @return date157 */158 public static Date getLastDayOfWeek() {159 gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);160 gregorianCalendar.setTime(new Date());161 gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek() + 6); // Monday162 return gregorianCalendar.getTime();163 }164 165 /**166 * 获取日期星期一日期167 * 168 * @param 指定日期169 * @return date170 */171 public static Date getFirstDayOfWeek(Date date) {172 if (date == null) {173 return null;174 }175 gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);176 gregorianCalendar.setTime(date);177 gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek()); // Monday178 return gregorianCalendar.getTime();179 }180 181 /**182 * 获取日期星期一日期183 * 184 * @param 指定日期185 * @return date186 */187 public static Date getLastDayOfWeek(Date date) {188 if (date == null) {189 return null;190 }191 gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);192 gregorianCalendar.setTime(date);193 gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek() + 6); // Monday194 return gregorianCalendar.getTime();195 }196 197 /**198 * 获取当前月的第一天199 * 200 * @return date201 */202 public static Date getFirstDayOfMonth() {203 gregorianCalendar.setTime(new Date());204 gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);205 return gregorianCalendar.getTime();206 }207 208 /**209 * 获取当前月的最后一天210 * 211 * @return212 */213 public static Date getLastDayOfMonth() {214 gregorianCalendar.setTime(new Date());215 gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);216 gregorianCalendar.add(Calendar.MONTH, 1);217 gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1);218 return gregorianCalendar.getTime();219 }220 221 /**222 * 获取指定月的第一天223 * 224 * @param date225 * @return226 */227 public static Date getFirstDayOfMonth(Date date) {228 gregorianCalendar.setTime(date);229 gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);230 return gregorianCalendar.getTime();231 }232 233 /**234 * 获取指定月的最后一天235 * 236 * @param date237 * @return238 */239 public static Date getLastDayOfMonth(Date date) {240 gregorianCalendar.setTime(date);241 gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);242 gregorianCalendar.add(Calendar.MONTH, 1);243 gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1);244 return gregorianCalendar.getTime();245 }246 247 /**248 * 获取日期前一天249 * 250 * @param date251 * @return252 */253 public static Date getDayBefore(Date date) {254 gregorianCalendar.setTime(date);255 int day = gregorianCalendar.get(Calendar.DATE);256 gregorianCalendar.set(Calendar.DATE, day - 1);257 return gregorianCalendar.getTime();258 }259 260 /**261 * 获取日期后一天262 * 263 * @param date264 * @return265 */266 public static Date getDayAfter(Date date) {267 gregorianCalendar.setTime(date);268 int day = gregorianCalendar.get(Calendar.DATE);269 gregorianCalendar.set(Calendar.DATE, day + 1);270 return gregorianCalendar.getTime();271 }272 273 /**274 * 获取当前年275 * 276 * @return277 */278 public static int getNowYear() {279 Calendar d = Calendar.getInstance();280 return d.get(Calendar.YEAR);281 }282 283 /**284 * 获取当前月份285 * 286 * @return287 */288 public static int getNowMonth() {289 Calendar d = Calendar.getInstance();290 return d.get(Calendar.MONTH) + 1;291 }292 293 /**294 * 获取当月天数295 * 296 * @return297 */298 public static int getNowMonthDay() {299 Calendar d = Calendar.getInstance();300 return d.getActualMaximum(Calendar.DATE);301 }302 303 /**304 * 获取时间段的每一天305 * 306 * @param 开始日期307 * @param 结算日期308 * @return 日期列表309 */310 public static ListgetEveryDay(Date startDate, Date endDate) {311 if (startDate == null || endDate == null) {312 return null;313 }314 // 格式化日期(yy-MM-dd)315 startDate = DateUtil.getDateFormat(DateUtil.getDateFormat(startDate));316 endDate = DateUtil.getDateFormat(DateUtil.getDateFormat(endDate));317 List dates = new ArrayList ();318 gregorianCalendar.setTime(startDate);319 dates.add(gregorianCalendar.getTime());320 while (gregorianCalendar.getTime().compareTo(endDate) < 0) {321 // 加1天322 gregorianCalendar.add(Calendar.DAY_OF_MONTH, 1);323 dates.add(gregorianCalendar.getTime());324 }325 return dates;326 }327 328 /**329 * 获取提前多少个月330 * 331 * @param monty332 * @return333 */334 public static Date getFirstMonth(int monty) {335 Calendar c = Calendar.getInstance();336 c.add(Calendar.MONTH, -monty);337 return c.getTime();338 }339 }