Java日期操作工具类

Java日期操作工具类教程

在平时工作中,我们会经常遇到日期之间相互转换的问题。日期转换看起来简单,但是在实际编写过程中还是比较复杂的。下面我们整理一些工具类,供您参考。

案例

按照月维度进行增加减

public static Date getDateForPlusMonth(){ LocalDate localDate = LocalDate.now(); //获取当前时间 localDate = localDate.plusMonths(-3); //这边是获取三个月之前的那一天, -3 这个数字可以随便填写,大于 0 表示后面时间 ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); Date retDate = Date.from(zonedDateTime.toInstant()); return retDate; }

描述

该工具类是以月为维度进行计算,如果相加或者相减,最后的月份都会停留在当前月减少或者增加的那个月,如果超过结果那个月的日子,会停留在最后一天。比如:5 月 30 号减少 3 个月,它会停留在 2 月 28 或者 29 号。

按照天维度进行增加减

public static Date getCurrentDateByPlusDays(Integer days) { LocalDate localDate = LocalDate.now(); //获取系统当前天 localDate = localDate.plusDays(days); //进行天相加减,大于 0 日期往后算,小于 0 日期往前算 ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); Date retDate = Date.from(zonedDateTime.toInstant()); return retDate; }

描述

以天为维度进行计算,获取当前时间前几天获取后几天的日期。

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { /** * 日期格式 年 月 如 2016-05 */ public static final String DATEFORMATMONTH = "yyyy-MM"; /** * 日期格式 年 月 日 如2016-05-29 */ public static final String DATEFORMATDAY = "yyyy-MM-dd"; /** * 日期格式年 月 日 时 分 秒 如 2016-05-29 19:40:00 */ public static final String DATEFORMATSECOND = "yyyy-MM-dd HH:mm:ss"; /** * 日期格式年 月 日 时 分 秒 毫秒 如2016-05-29 20:40:00 110 */ public static final String DATEFORMATMILLISECOND = "yyyy-MM-dd HH:mm:ss SSS"; /** * 按指定的格式,把Date转换成String 如date为null,返回null * * @param date Date参数 * @param format 日期格式 * @return String */ public static String formatDate2Str(Date date, String format) { if (date == null) { return null; } return new SimpleDateFormat(format).format(date); } /** * 按指定的格式,把string转换成Date 如string为空或null,返回null * * @param string * @param format * @return * @throws ParseException */ public static Date paraseStr2Date(String string, String format) throws ParseException { if (string == null || "".equals(string.trim())) { return null; } return new SimpleDateFormat(format).parse(string); } /** * 获取当前时间天 * * @return */ public static Date getCurrentDay() { Date currentTime = new Date(); Date nextDay = org.apache.commons.lang3.time.DateUtils.ceiling(currentTime, Calendar.DATE); Date currentDate = org.apache.commons.lang3.time.DateUtils.addDays(nextDay, -1); return currentDate; } /** * 获取明天数据 * * @return */ public static Date getNextDay() { return org.apache.commons.lang3.time.DateUtils.ceiling(new Date(), Calendar.DATE); } /** * 获取昨天数据 * * @return */ public static Date getPreDay() { Date currentTime = new Date(); Date nextDay = org.apache.commons.lang3.time.DateUtils.ceiling(currentTime, Calendar.DATE); Date currentDate = org.apache.commons.lang3.time.DateUtils.addDays(nextDay, -2); return currentDate; } /** * 获取当前系统时间 * * @return */ public static Date getCurrentSysTime() { Calendar calender = Calendar.getInstance(); return calender.getTime(); } /** * 根据当前日期返回本月的最后一天 * * @param someDate * @return */ public static Date getLastDayOfMonthFromDate(Date someDate) { final Calendar ca = Calendar.getInstance(); ca.setTime(someDate); // someDate 为你要获取的那个月的时间 ca.set(Calendar.DAY_OF_MONTH, 1); ca.add(Calendar.MONTH, 1); ca.add(Calendar.DAY_OF_MONTH, -1); return ca.getTime(); } /** * 得到本月最后一天的日期 */ public static Date getLastDayOfMonthFromStr(String dateStr) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMATMONTH); Date date = sdf.parse(dateStr); Calendar ca = Calendar.getInstance(); ca.setTime(date); ca.set(Calendar.DAY_OF_MONTH, 1); ca.add(Calendar.MONTH, 1); ca.add(Calendar.DAY_OF_MONTH, -1); return ca.getTime(); } /** * 取一天的开始时间 精确到毫秒 * * @param date * @return * @throws Exception */ public static Date getDayFirstTime(Date date) throws Exception { if (date == null) { return null; } final String str = formatDate2Str(date, DATEFORMATDAY) + " 00:00:00 000"; return paraseStr2Date(str, DATEFORMATMILLISECOND); } /** * 取一天的结束时间 精确到毫秒 * * @param date * @return * @throws Exception */ public static Date getDayLastTime(java.util.Date date) throws Exception { if (date == null) { return null; } final String str = formatDate2Str(date, DATEFORMATDAY) + " 23:59:59 999"; return paraseStr2Date(str, DATEFORMATMILLISECOND); } }