高可用的日期工具类

xiaoxiao2021-02-28  111

package com.date.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { // 获得当天0点时间 public static Date getTimesmorning() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } // 获得当天24点时间 public static Date getTimesnight() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 24); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } // 获得本周一0点时间 public static Date getTimesWeekmorning() { Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return cal.getTime(); } // 获得本周日24点时间 public static Date getTimesWeeknight() { Calendar cal = Calendar.getInstance(); cal.setTime(getTimesWeekmorning()); cal.add(Calendar.DAY_OF_WEEK, 7); return cal.getTime(); } // 获得本周开始时间 public static Date getTimesWeekStart() { Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return cal.getTime(); } // 获得本周结束时间 public static Date getTimesWeekEed() { Calendar cal = Calendar.getInstance(); cal.setTime(getTimesWeekmorning()); cal.add(Calendar.DAY_OF_WEEK, 7); return cal.getTime(); } // 获得本月第一天0点时间 public static Date getTimesMonthmorning() { Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); return cal.getTime(); } // 获得本月最后一天24点时间 public static Date getTimesMonthnight() { Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 24); return cal.getTime(); } /** * 当前季度的开始时间 * @return */ public static Date getCurrentQuarterStartTime() { Calendar cal = Calendar.getInstance(); int currentMonth = cal.get(Calendar.MONTH)+1; Date now = null; Integer month = null; try { if (currentMonth >= 1 && currentMonth <= 3) month =1; else if (currentMonth >= 4 && currentMonth <= 6) month =4; else if (currentMonth >= 7 && currentMonth <= 9) month =7; else if (currentMonth >= 10 && currentMonth <= 12) month =10; cal.set(cal.get(Calendar.YEAR), month-1, 1, 0, 0, 0); now = cal.getTime(); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前季度的结束时间 * * @return */ public static Date getCurrentQuarterEndTime() { Calendar cal = Calendar.getInstance(); int currentMonth = cal.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 3) { cal.set(cal.get(Calendar.YEAR), 2, 31, 23, 59, 59); } else if (currentMonth >= 4 && currentMonth <= 6) { cal.set(cal.get(Calendar.YEAR), 5, 30, 23, 59, 59); } else if (currentMonth >= 7 && currentMonth <= 9) { cal.set(cal.get(Calendar.YEAR), 8, 30, 23, 59, 59); } else if (currentMonth >= 10 && currentMonth <= 12) { cal.set(cal.get(Calendar.YEAR), 11, 31, 23, 59, 59); } now = cal.getTime(); } catch (Exception e) { e.printStackTrace(); } return now; } public static String getFormatDate(Date d, String formatString){ String currentDate = ""; SimpleDateFormat format1 = new SimpleDateFormat(formatString); currentDate = format1.format(d); return currentDate; } public static String getCurrentDate(){ return getFormatDate(new Date(), "yyyy-MM-dd"); } public static String getCurrentYear(){ return getFormatDate(new Date(), "yyyy"); } public static String getCurrentMonth(){ return getFormatDate(new Date(), "MM"); } public static String getFormateDateAll(){ return getFormatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); } public static String getFormateDateSimple(){ return getFormatDate(new Date(), "yyyyMMddHHmmss"); } public static Date getDateAfterDay(Date d, int day){ Calendar now = Calendar.getInstance(); now.setTime(d); now.set(5, now.get(5) + day); return now.getTime(); } public static Date getDateAfterHour(Date d, int hour){ Calendar now = Calendar.getInstance(); now.setTime(d); now.set(10, now.get(10) + hour); return now.getTime(); } public static Date getDateAfterMinute(Date d, int minutes){ Calendar now = Calendar.getInstance(); now.setTime(d); now.set(12, now.get(12) + minutes); return now.getTime(); } public static Date getDateAfterSecond(Date d, int second){ Calendar now = Calendar.getInstance(); now.setTime(d); now.set(13, now.get(13) + second); return now.getTime(); } public static boolean dateCompare(Date date1, Date date2){ boolean dateComPareFlag = true; if (date2.compareTo(date1) != 1) { dateComPareFlag = false; } return dateComPareFlag; } public static long dateMinus(Date startTime, Date endTime){ return endTime.getTime() - startTime.getTime(); } public static String time(String startTime, String endTime) throws ParseException{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date outdate = sdf.parse(endTime); Date indate = sdf.parse(startTime); long totalhours = (outdate.getTime() - indate.getTime()) / 3600000L; long totalminutes = (outdate.getTime() - indate.getTime()) / 60000L - totalhours * 60L; long totalseconds = (outdate.getTime() - indate.getTime()) / 1000L - totalminutes * 60L; String total_time = totalhours + "时" + totalminutes + "分" + totalseconds + "秒"; return total_time; } public static String examRemainTime(String startTime, String endTime) throws ParseException{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date outdate = sdf.parse(endTime); Date indate = sdf.parse(startTime); long totalminutes = (outdate.getTime() - indate.getTime()) / 60000L; long totalseconds = (outdate.getTime() - indate.getTime()) / 1000L - totalminutes * 60L; String remainTime = totalminutes + "#" + totalseconds; return remainTime; } public static Date getFormatStr(String dateStr, String formatString) { SimpleDateFormat format1 = new SimpleDateFormat(formatString); Date date = null; try { date = format1.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return date; } @SuppressWarnings("deprecation") public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("当前时间:"+ new Date().toLocaleString()); System.out.println("当天0点时间:"+ getTimesmorning().toLocaleString()); System.out.println("当天24点时间:"+ getTimesnight().toLocaleString()); System.out.println("本周周一0点时间:"+ getTimesWeekmorning().toLocaleString()); System.out.println("本周周日24点时间:"+ getTimesWeeknight().toLocaleString()); System.out.println("本月初0点时间:"+ getTimesMonthmorning().toLocaleString()); System.out.println("本月未24点时间:"+ getTimesMonthnight().toLocaleString()); System.out.println("本周周一0点时间:"+ getTimesWeekStart().toLocaleString()); System.out.println("本周周日24点时间:"+ getTimesWeekEed().toLocaleString()); System.out.println("本季度开始时间:"+ getCurrentQuarterStartTime().toLocaleString()); System.out.println("本季度结束时间:"+ getCurrentQuarterEndTime().toLocaleString()); } }
转载请注明原文地址: https://www.6miu.com/read-46825.html

最新回复(0)