java日期的往前推几天,关于时间的常用方法 您所在的位置:网站首页 calendar时间计算 java日期的往前推几天,关于时间的常用方法

java日期的往前推几天,关于时间的常用方法

2023-12-09 18:30| 来源: 网络整理| 查看: 265

在平常的开发中,经常会接到XX天后才能做XXX事情,默认查询XX月之内的订单,还有定时任务中,抓取距现在10分钟内的某淘平台产生的订单等等需求,现在我就对这些时间操作进行一个总结。

主要涉及到的类: java.util.Calendar void java.util.Calendar.add(int field, int amount) :对日历进行设置

void java.util.Calendar.set(int field, int value):对日历往后推XXX

field常用的枚举: Calendar.YEAR —-年 Calendar.MONTH—-月 Calendar.DATE—-日 Calendar.HOUR_OF_DAY—-小时 Calendar.MINUTE—-分 calendar.SECOND—-秒

先介绍下 void java.util.Calendar.add(int field, int amount) 上测试代码1

@Test public void setTest(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date nowDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(nowDate); Date updateDate1 = calendar.getTime(); System.out.println("变化后的时间1="+sdf.format(updateDate1)); //1 月份的设置,0为1月份算起, calendar.set(2017, 10, 10, 10, 10, 10); Date updateDate2 = calendar.getTime(); System.out.println("变化后的时间2="+sdf.format(updateDate2)); //2 如果月日时分秒字段 超过最大数,会向前个进制一位,如月份设置成12,则年份就+1,月份变成1月份 calendar.set(2017, 12, 10, 10, 10, 10); Date updateDate3 = calendar.getTime(); System.out.println("变化后的时间3="+sdf.format(updateDate3)); //2017年11月份最多有30日,设置成31,进制到月 calendar.set(2017, 10, 31, 10, 10, 10); Date updateDate4 = calendar.getTime(); System.out.println("变化后的时间4="+sdf.format(updateDate4)); calendar.set(2017, 10, 31, 10, 10, 10); } 控制台输出: 变化后的时间1=2017-10-27 15:16:34 变化后的时间2=2017-11-10 10:10:10 变化后的时间3=2018-01-10 10:10:10 变化后的时间4=2017-12-01 10:10:10

测试代码2

@Test public void setTest1(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date nowDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(nowDate); Date updateDate1 = calendar.getTime(); System.out.println("当前时间="+sdf.format(updateDate1)); //往前推一个月 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)-1); Date updateDate2 = calendar.getTime(); System.out.println("往前推1个月的时间"+sdf.format(updateDate2)); //往后推13个月 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)+13); Date updateDate3 = calendar.getTime(); System.out.println("往后推13个月的时间="+sdf.format(updateDate3)); //往后推一天 calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)+1); Date updateDate4 = calendar.getTime(); System.out.println("往后推1天的时间="+sdf.format(updateDate4)); } 控制台输出: 当前时间=2017-10-27 16:30:32 往前推1个月的时间2017-09-27 16:30:32 往后推13个月的时间=2018-10-27 16:30:32 往后推1天的时间=2018-10-28 16:30:32

结论: 1、set()方法是可以设置指定的日历时间,也可以利用来推算往前往后的时间,但代码看过去比较繁琐,最好用add()的方法进行推算 2、如果月、日、时分秒的大小超过了常规范围,就会向前一位进制,比如月份设置calendar.set(2017, 12, 10, 10, 10, 10); 12超过了11的范围(0为一月),则得到的结果为2018-01-10 10:10:10

void java.util.Calendar.add(int field, int amount)

测试代码3

@Test public void addTest(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date nowDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(nowDate); Date updateDate1 = calendar.getTime(); System.out.println("当前时间="+sdf.format(updateDate1)); //往前推1个月 calendar.add(Calendar.MONTH, -1); Date updateDate2 = calendar.getTime(); System.out.println("往前推1个月的时间"+sdf.format(updateDate2)); //往后推13个月 calendar.add(Calendar.MONTH, 13); Date updateDate3 = calendar.getTime(); System.out.println("往后推13个月的时间="+sdf.format(updateDate3)); //往前推1天 calendar.add(Calendar.DATE, -1); Date updateDate4 = calendar.getTime(); System.out.println("往前推1天的时间"+sdf.format(updateDate4)); //往前推1小时 calendar.add(Calendar.HOUR_OF_DAY, -1); Date updateDate5 = calendar.getTime(); System.out.println("往前推1小时的时间="+sdf.format(updateDate5)); //往前推1分钟 calendar.add(Calendar.MINUTE, -1); Date updateDate6 = calendar.getTime(); System.out.println("往前推1分钟的时间="+sdf.format(updateDate6)); //往前推1秒钟 calendar.add(Calendar.SECOND, -1); Date updateDate7 = calendar.getTime(); System.out.println("往前推1秒的时间="+sdf.format(updateDate7)); } **控制台输出:** 当前时间=2017-10-27 16:35:58 往前推1个月的时间2017-09-27 16:35:58 往后推13个月的时间=2018-10-27 16:35:58 往前推1天的时间2018-10-26 16:35:58 往前推1小时的时间=2018-10-26 15:35:58 往前推1分钟的时间=2018-10-26 15:34:58 往前推1秒的时间=2018-10-26 15:34:57

总结: 1、set()方法虽然也可以往前后推算时间,但写起来比较繁杂又容易让人误解,建议用来设置指定日期就好了. 2、add()方法主要用来往前后推算时间,简单移动,强烈推荐使用

下面是我自己写的一个关于时间的工具类,发出来共享下,多多指教:

package com.lh.drest.electronicInvoice.utils; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class DateUtil { public final static String DATEFORMATYMD = "yyyy-MM-dd"; public final static String DATEFORMATHMS = "yyyy-MM-dd HH:mm:ss"; public final static String DATEFORMATHMSS = "yyyy-MM-dd HH:mm:ss:SSS"; public final static String DATEFORMATCH = "yyyy年MM月dd日"; public final static String DATEFORMATMCH = "yyyy年MM月dd日 HH时mm分ss秒"; public final static String DATEFORMATSLASH = "yyyy/MM/dd HH:mm:ss"; public static DateFormat FORMAT_TIME = null; public static Calendar c = Calendar.getInstance();; public DateUtil() { } /** * 判断是否闰年 * * @param year * @return */ public static boolean LeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return true; } else { return false; } } public static boolean isDayOfBegin(Date date) { String temp = getDate(date, 2, ""); if (temp.substring(11, temp.length()).equals("00:00:00")) { return true; } else { return false; } } /** * 得到格式化后的日期 * * @param date需要格式化的日期 * @param marked * 标示格式 (1:yyyy-MM-dd; 2:yyyy-MM-dd HH:mm:ss; 3:yyyy年MM月dd日; * 4:yyyy年MM月dd日 HH时mm分ss秒 5:自定义格式) 当marked=5时,参数dateFormat为必填项 * 默认状态为2 * */ public static String getDate(Date date, int marked, String dateFormat) { switch (marked) { case 1: FORMAT_TIME = new SimpleDateFormat(DATEFORMATYMD); break; case 2: FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS); break; case 3: FORMAT_TIME = new SimpleDateFormat(DATEFORMATCH); break; case 4: FORMAT_TIME = new SimpleDateFormat(DATEFORMATMCH); break; case 5: FORMAT_TIME = new SimpleDateFormat(dateFormat); break; case 6: FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMSS); break; default: FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS); break; } if (date == null) date = new Date(); return FORMAT_TIME.format(date); } /** * 获得当前的年 * */ public static int getYear() { return c.get(Calendar.YEAR); } /** * 获得当前月份 * * @return */ public static int getMonth() { return c.get(Calendar.MONTH) + 1; } /** * 获取当期日期的前一个月 * */ public static int getPreMoth() { c.clear(); c.setTime(new Date()); c.add(Calendar.MONTH, -1); return c.get(Calendar.MONTH) + 1; } /** * 获得今天在本年的第几天 * * @return */ public static int getDayOfYear() { return c.get(Calendar.DAY_OF_YEAR); } /** * 获得今天在本月的第几天(获得当前日) * * @return */ public static int getDayOfMonth() { return c.get(Calendar.DAY_OF_MONTH); } /** * 获得今天在本周的第几天 * * @return */ public static int getDayOfWeek() { return c.get(Calendar.DAY_OF_WEEK); } /** * 获得今天是这个月的第几周 * * @return */ public static int getWeekOfMonth() { return c.get(Calendar.DAY_OF_WEEK_IN_MONTH); } /** * 得到二个日期间的间隔毫秒数 */ public static long getTwoDayTimes(String sj1, String sj2, boolean second) { SimpleDateFormat myFormatter; if (second) myFormatter = new SimpleDateFormat(DATEFORMATHMS); else myFormatter = new SimpleDateFormat(DATEFORMATYMD); long day = 0; try { Date date = myFormatter.parse(sj1); Date mydate = myFormatter.parse(sj2); day = date.getTime() - mydate.getTime(); } catch (Exception e) { } return day; } /** * 得到二个日期间的间隔毫秒数 sj1-sj2 */ public static long getTwoDayTimes(Date sj1, Date sj2) { long day = 0; try { day = sj1.getTime() - sj2.getTime(); } catch (Exception e) { } return day; } /** * 两个时间之间的天数 * * @param date1 * @param date2 * @return date1-date2 */ public static long getDays(String date1, String date2) { if (date1 == null || date1.equals("")) return 0; if (date2 == null || date2.equals("")) return 0; // 转换为标准时间 long day = getTwoDayTimes(date1, date2, false) / (24 * 60 * 60 * 1000); return day; } /** * 两个时间之间的天数 * * @param date1 * @param date2 * @return date1-date2 */ public static long getDays(Date date1, Date date2) { if (date1 == null) return 0; if (date2 == null) return 0; // 转换为标准时间 long day = getTwoDayTimes(date1, date2) / (24 * 60 * 60 * 1000); return day; } /** * 计算当月最后一天,返回字符串 * */ public static String getDefaultDay() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMATYMD); Calendar lastDate = Calendar.getInstance(); lastDate.set(Calendar.DATE, 1);// 设为当前月的1号 lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号 lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天 str = sdf.format(lastDate.getTime()); return str; } /** * 获得本年有多少天 * */ public static int getMaxYear() { Calendar cd = Calendar.getInstance(); cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天 cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滚一天。 int MaxYear = cd.get(Calendar.DAY_OF_YEAR); return MaxYear; } /** * 获取星期几 * */ public static String getWeek(Date date) { Calendar c1 = Calendar.getInstance(); if (date != null) { c1.setTime(date); } return new SimpleDateFormat("EEEE").format(c1.getTime()); } /** * 获取星期几 * * @param strDate * 字符串. * @param marked * 标示格式 (1:yyyy-MM-dd; 2:yyyy-MM-dd HH:mm:ss; 3:yyyy年MM月dd日; * 4:yyyy年MM月dd日 HH时mm分ss秒 5:自定义格式) 当marked=5时,参数dateFormat为必填项 * 默认状态为2 * @param format * 转换格式如:"yyyy-MM-dd HH:mm:ss" * */ public static String getWeek(String strDate, int marked, String format) { return getWeek(parseDate(strDate, marked, format)); } // 获取下星期一 public static String getNextMonday(String strDate) { Calendar c1 = Calendar.getInstance(); c1.setTime(parseDate(strDate, 1, "")); int mondayPlus = c1.get(Calendar.DAY_OF_WEEK) - 1; if (mondayPlus == 0) { mondayPlus = 7; } c1.add(GregorianCalendar.DATE, 8 - mondayPlus); Date monday = c1.getTime(); DateFormat df = DateFormat.getDateInstance(); String preMonday = df.format(monday); return preMonday; } /** * 获取当前日期的上一个星期一日期 * @param strDate * @return */ public static String getPreMonday() { Calendar calendar=Calendar.getInstance(); calendar.add(Calendar.WEEK_OF_MONTH, -1); calendar.set(Calendar.DAY_OF_WEEK, 2); Date firstWeek = calendar.getTime(); String preMonday=DateUtil.getDate(firstWeek, 1, null); return preMonday; } /** * 根据当前时间的毫秒数(相对于January 1, 1970 00:00:00),取当前时间的字符串 * * @param marked * 标示格式 (1:yyyy-MM-dd; 2:yyyy-MM-dd HH:mm:ss; 3:yyyy年MM月dd日; * 4:yyyy年MM月dd日 HH时mm分ss秒 5:自定义格式) 当marked=5时,参数dateFormat为必填项 * 默认状态为2 * @param format * 转换格式如:"yyyy-MM-dd HH:mm:ss" * @return */ public static String changTimeMillisToStr(Long longDate, int marked, String format) { switch (marked) { case 1: FORMAT_TIME = new SimpleDateFormat(DATEFORMATYMD); break; case 2: FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS); break; case 3: FORMAT_TIME = new SimpleDateFormat(DATEFORMATCH); break; case 4: FORMAT_TIME = new SimpleDateFormat(DATEFORMATMCH); break; case 5: FORMAT_TIME = new SimpleDateFormat(format); break; default: FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS); break; } return FORMAT_TIME.format(longDate); } /** * 格式化字符串为日期的函数. * * @param strDate * 字符串. * @param marked * 标示格式 (1:yyyy-MM-dd; 2:yyyy-MM-dd HH:mm:ss; 3:yyyy年MM月dd日; * 4:yyyy年MM月dd日 HH时mm分ss秒 5:自定义格式) 当marked=5时,参数dateFormat为必填项 * 默认状态为2 * @param format * 转换格式如:"yyyy-MM-dd HH:mm:ss" * @return 字符串包含的日期,失败则返回当前日期 */ public static Date parseDate(String strDate, int marked, String format) { try { if (strDate == null || strDate.equals("") || strDate.equals("null") || strDate.trim().length() == 0) { return new Date(); } switch (marked) { case 1: FORMAT_TIME = new SimpleDateFormat(DATEFORMATYMD); break; case 2: FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS); break; case 3: FORMAT_TIME = new SimpleDateFormat(DATEFORMATCH); break; case 4: FORMAT_TIME = new SimpleDateFormat(DATEFORMATMCH); break; case 5: FORMAT_TIME = new SimpleDateFormat(format); break; case 6: FORMAT_TIME = new SimpleDateFormat(format); break; default: FORMAT_TIME = new SimpleDateFormat(DATEFORMATSLASH); break; } return FORMAT_TIME.parse(strDate); } catch (Exception e) { e.printStackTrace(); return new Date(); } } public static int getYear(String strDate, int marked, String format) { Date d = parseDate(strDate, marked, format); Calendar c = Calendar.getInstance(); c.setTime(d); return c.get(Calendar.YEAR); } public static int getMonth(String strDate, int marked, String format) { Date d = parseDate(strDate, marked, format); Calendar c = Calendar.getInstance(); c.setTime(d); return c.get(Calendar.MONTH) + 1; } /** * 将字符串的 yyyy-mm-dd hh:mm:ss 翻译成数据库中的Long型 * * @param strDate * @param marked * 标示格式 (1:yyyy-MM-dd; 2:yyyy-MM-dd HH:mm:ss; 3:yyyy年MM月dd日; * 4:yyyy年MM月dd日 HH时mm分ss秒 5:自定义格式) 当marked=5时,参数dateFormat为必填项 * 默认状态为2 * @param 自定义格式 * @return */ public static Long parseString2Long(String strDate, int marked, String format) { return new Long(parseDate(strDate, marked, format).getTime()); } public static Long parseDate2Long(Date date) { return new Long(date.getTime()); } /** * 比较当前时间和给定的时间(只比较时、分、秒) * */ public static int compHMS(String hmsStart, String hmsEnd) { Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH) + 1; int day = c.get(Calendar.DATE); Date date = c.getTime(); hmsStart = Integer.toString(year) + "-" + Integer.toString(month) + "-" + Integer.toString(day) + " " + hmsStart; hmsEnd = Integer.toString(year) + "-" + Integer.toString(month) + "-" + Integer.toString(day) + " " + hmsEnd; Date dateStart = parseDate(hmsStart, 2, ""); Date dateEnd = parseDate(hmsEnd, 2, ""); if (date.getTime() >= dateStart.getTime() && date.getTime()


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有