Java Date工具类
Date 工具类
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DateUtil { static Logger logger = LoggerFactory.getLogger(DateUtil.class); /** * @param date yyyyMMdd format * @return the total days of this month */ public static Integer dayLimit(String date) { Integer year = new Integer(date.substring(0, 4)); String month = date.substring(4, 6); Boolean isLeapYear = false; String sWith31Days = "|01|03|05|07|08|10|12|"; String sWith30Days = "|04|06|09|11|"; String sWithFeb = "|02|"; Integer dayLimt = 31; // check if this year is leap year if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { isLeapYear = true; } else { isLeapYear = false; } // get total days of this month if (sWith31Days.contains(month)) { dayLimt = 31; } else if (sWith30Days.contains(month)) { dayLimt = 30; } else if (sWithFeb.contains(month)) { if (isLeapYear) { dayLimt = 29; } else { dayLimt = 28; } } return dayLimt; } public static String yesterday() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_YEAR, -1); String yesterday = simpleDateFormat.format(calendar.getTime()); return yesterday; } public static String today() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); String yesterday = simpleDateFormat.format(new Date()); return yesterday; } //某天的前一天 public static String beforeDate(String thisDate) throws ParseException { // get date before report date SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); Calendar calendar = Calendar.getInstance(); Date date = simpleDateFormat.parse(thisDate); calendar.setTime(date); calendar.add(Calendar.DATE, -1); String beforeDate = simpleDateFormat.format(calendar.getTime()); return beforeDate; } //Y-years 几年前 public static Date beforeYears(Integer years) { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.YEAR, -years); Date date = calendar.getTime(); return date; } //M-months 几月前 public static Date beforeMonths(Integer months) { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MONTH, -months); Date date = calendar.getTime(); return date; } //T-days 几天前 public static Date beforeDays(Integer days) { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DATE, -days); Date date = calendar.getTime(); return date; } public static SimpleDateFormat getSimpleDateFormat() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); return simpleDateFormat; } //date2String public static String date2String(Date date) { String stringNow = getSimpleDateFormat().format(date); // 把当前时间转为字符串,字符串格式为yyyy-MM-dd logger.info("stringNow is {}", stringNow); return stringNow; } //string2Date public static Date string2Date(String time) throws ParseException { // String time = ""2017-01-04 11:22:33:000""; Date date = getSimpleDateFormat().parse(time); return date; } //sleep static void sleep(Integer beginHour, Integer beginMin, Integer endHour, Integer endMin, Integer period) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar date = Calendar.getInstance(); Date nowDate = date.getTime(); Calendar begin = new GregorianCalendar(); begin.set(Calendar.HOUR_OF_DAY, beginHour); begin.set(Calendar.MINUTE, beginMin); begin.set(Calendar.SECOND, 0); begin.set(Calendar.MILLISECOND, 0); Date beginDate = begin.getTime(); Calendar end = new GregorianCalendar(); end.set(Calendar.HOUR_OF_DAY, endHour); end.set(Calendar.MINUTE, endMin); end.set(Calendar.SECOND, 0); end.set(Calendar.MILLISECOND, 0); Date endDate = end.getTime(); logger.info("begin time is {}", simpleDateFormat.format(beginDate)); logger.info("end time is {}", simpleDateFormat.format(endDate)); logger.info("before sleep time is {}", simpleDateFormat.format(nowDate)); Boolean result = date.after(begin) && date.before(end); if (result) { try { Thread.sleep(period); } catch (InterruptedException e) { } } Calendar test = Calendar.getInstance(); Date testDate = test.getTime(); logger.info("after sleep time is {}", simpleDateFormat.format(testDate)); } public static void main(String[] args) throws ParseException { Date date = beforeDays(2); date2String(date); } }