2025年java时间操作工具类(判断时间是周几、多少号、几月、几年,计算时间差、计算时间相差几天,获取今天、昨天、近7天、本周、本月、本季、本年、去年开始时间和结束时间,获取本周、本月、本季、本年集合)

java时间操作工具类(判断时间是周几、多少号、几月、几年,计算时间差、计算时间相差几天,获取今天、昨天、近7天、本周、本月、本季、本年、去年开始时间和结束时间,获取本周、本月、本季、本年集合)做数据统计时经常用的时间判断 获得当前月开始时间 return 结果 public static Date getCurrentMo Calendar cal Calendar getInstance cal set cal get

大家好,我是讯享网,很高兴认识大家。

做数据统计时经常用的时间判断

 / * 获得当前月开始时间 * @return 结果 */ public static Date getCurrentMonthStartTime() { 
    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(); } / * 获得当前月结束时间 * @return 结果 */ public static Date getCurrentMonthEndTime() { 
    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, 23); return cal.getTime(); } 

讯享网
讯享网 / * 获取当前季度开始时间 * @return 结果 */ public static Date getCurrentQuarterStartTime() { 
    Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); Date now = null; try { 
    if (currentMonth >= 1 && currentMonth <= 3) { 
    c.set(Calendar.MONTH, 0); } else if (currentMonth >= 4 && currentMonth <= 6) { 
    c.set(Calendar.MONTH, 3); } else if (currentMonth >= 7 && currentMonth <= 9) { 
    c.set(Calendar.MONTH, 4); } else if (currentMonth >= 10 && currentMonth <= 12) { 
    c.set(Calendar.MONTH, 9); } c.set(Calendar.DATE, 1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); } catch (Exception e) { 
    e.printStackTrace(); } return now; } / * 获取本季度结束时间 * @return 结果 */ public static Date getCurrentQuarterEndTime() { 
    Calendar cal = Calendar.getInstance(); cal.setTime(getCurrentQuarterStartTime()); cal.add(Calendar.MONTH, 3); return cal.getTime(); } 
 / * 获得本周开始时间 * @return 结果 */ public static Date getCurrentWeekStartTime() { 
    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(); } / * 获得本周结束时间 * @return 结果 */ public static Date getCurrentWeekEndTime() { 
    Calendar cal = Calendar.getInstance(); cal.setTime(getCurrentWeekStartTime()); cal.add(Calendar.DAY_OF_WEEK, 6); return cal.getTime(); } 
讯享网 / * 获取当前年开始时间 * @return 结果 */ public static Date getCurrentYearStartTime() { 
    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 + 1, cal.getActualMinimum(Calendar.YEAR)); return cal.getTime(); } / * 获取本年结束时间 * @return 结果 */ public static Date getCurrentYearEndTime() { 
    Calendar cal = Calendar.getInstance(); cal.setTime(getCurrentYearStartTime()); cal.add(Calendar.YEAR, 1); return cal.getTime(); } 
 / * 获取去年开始时间 * @return 结果 */ public static Date getLastYearStartTime() { 
    Calendar cal = Calendar.getInstance(); cal.setTime(getCurrentYearStartTime()); cal.add(Calendar.YEAR, -1); return cal.getTime(); } / * 获取去年结束时间 * @return 结果 */ public static Date getLastYearEndTime() { 
    Calendar cal = Calendar.getInstance(); cal.setTime(getCurrentYearStartTime()); cal.add(Calendar.YEAR, 0); return cal.getTime(); } 
讯享网 / * 获取今天开始时间 * @return 结果 */ public static Date getCurrentDayTime() { 
    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(); } / * 获得昨天开始时间 * @return 结果 */ public static Date getYesterdayTime() { 
    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); Date time = cal.getTime(); cal.setTimeInMillis(time.getTime() - 3600 * 24 * 1000); return cal.getTime(); } 
 / * 获得当天近7天时间 * @return 结果 */ public static Date getWeekFromNow() { 
    Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(getCurrentDayTime().getTime() - 3600 * 24 * 1000 * 7); return cal.getTime(); } 
讯享网 / * 精准计算俩个时间差 * @param startTime 开始时间 * @param endTime 结束时间 * @return 结果 */ public static String computationTime(Date startTime, Date endTime) { 
    try { 
    long diff = endTime.getTime() - startTime.getTime(); long day = diff / ND; long hour = diff % ND / NH; long min = diff % ND % NH / NM; long sec = diff % ND % NH % NM / 1000; String str = day + "天" + hour + "小时" + min + "分钟" + sec + "秒"; return str; } catch (Exception e) { 
    return null; } } / * 计算俩个时间相差几天 * @param startTime 开始时间 * @param endTime 结束时间 * @return 结果 */ public static Long computationDayTime(Date startTime, Date endTime) { 
    try { 
    long diff = endTime.getTime() - startTime.getTime(); long day = diff / ND; return day; } catch (Exception e) { 
    return null; } } 
 / * 获取当前周集合 * @return 结果 */ public static List<String> getWeekList() { 
    List<String> weekList = new LinkedList<>(); weekList.add("周一"); weekList.add("周二"); weekList.add("周三"); weekList.add("周四"); weekList.add("周五"); return weekList; } / * 获取当前月集合 * @return 结果 */ public static List<String> getMonthList() { 
    Long i = computationDayTime(getCurrentMonthStartTime(), getCurrentMonthEndTime()); List<String> monthList = new ArrayList<>(); for (int j = 1; j <= i; j++) { 
    monthList.add(j + "日"); } return monthList; } / * 获取当前季集合 * @return 结果 */ public static List<String> getCurrentQuarterList() { 
    Date time = getCurrentQuarterStartTime(); Calendar cal = Calendar.getInstance(); cal.setTime(time); //获取本季的开始月份 int month = cal.get(Calendar.MONTH) + 1; List<String> currentQuarterList = new ArrayList<>(); for (int j = 0; j < 3; j++) { 
    currentQuarterList.add(month + "月"); month++; } return currentQuarterList; } / * 获取本年集合 * @return 结果 */ public static List<String> getYearList() { 
    List<String> yearList = new ArrayList<>(); for (int j = 1; j <= 12; j++) { 
    yearList.add(j + "月"); } return yearList; } 
讯享网 / * 判断当前时间是周几 * @param time 时间 * @return 结果 */ public static String dayForWeek(Date time) { 
    Calendar cal = Calendar.getInstance(); String[] weekDays = { 
   "7", "1", "2", "3", "4", "5", "6"}; try { 
    cal.setTime(time); } catch (Exception e) { 
    e.printStackTrace(); } // 指示一个星期中的某天。 int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) { 
    w = 0; } return weekDays[w]; } //判断时间是多少号 public static String timeForMonth(Date time) { 
    Calendar cal = Calendar.getInstance(); String[] monthDays = { 
   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}; try { 
    cal.setTime(time); } catch (Exception e) { 
    e.printStackTrace(); } int w = cal.get(Calendar.DAY_OF_MONTH) - 1; if (w < 0) { 
    w = 0; } return monthDays[w]; } //判断时间是几月 public static String equalsTimeForMonths(Date time) { 
    Calendar cal = Calendar.getInstance(); String[] months = { 
   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}; try { 
    cal.setTime(time); } catch (Exception e) { 
    e.printStackTrace(); } int w = cal.get(Calendar.MONTH); if (w < 0) { 
    w = 0; } return months[w]; } //判断时间是几年 public static String equalsTimeForYear(Date time) { 
    Calendar cal = Calendar.getInstance(); try { 
    cal.setTime(time); } catch (Exception e) { 
    e.printStackTrace(); } int w = cal.get(Calendar.YEAR); if (w < 0) { 
    w = 0; } return String.valueOf(w); } 
 //获取当前月有多少天 private static Integer getMonthLastDay(Date date) { 
    GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); return cal.getActualMaximum(Calendar.DAY_OF_MONTH); } //获取当前年有多少天 private static Integer getYearLastDay(Date date) { 
    GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); return cal.getActualMaximum(Calendar.DAY_OF_YEAR); } 
小讯
上一篇 2025-02-11 08:19
下一篇 2025-03-22 07:27

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/60387.html