在这里插入图片描述
@[toc]

1.日期毫秒数转日期字符串

@Test
public void millisecondsToDate() throws ParseException {
   SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   System.out.println(simpleDateFormat.format(new Date(1639381826)));
}

2.检测一个字符串是否是时间格式

/**
*  检测一个字符串是否是时间格式
   * @param str 请求字符串
   * @author liudz
   * @date 2019/12/17
   * @return 执行结果
   **/
public static boolean isValidDate(String str) {
    boolean convertSuccess = true;
    // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
    SimpleDateFormat format = new SimpleDateFormat("HH:mm");
    try {
        format.setLenient(false);
        format.parse(str);
    } catch (Exception e) {
        convertSuccess = false;
    }
    return convertSuccess;
}

3.Long转String(时间毫秒数转日期格式字符串)

//Long转String(时间毫秒数转日期格式字符串)
@Test
void millisecondsTransformString()  {
    long milliSecond = 1570650412089L;
    Date date = new Date();
    date.setTime(milliSecond);
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));   
    //2019-10-10 03:46:52
}

4.String转Long(日期格式字符串转为时间毫秒数)

//String转Long(日期格式字符串转为时间毫秒数)
@Test
void stringTransformMilliseconds()  {
    SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String time = "2020-10-11 10:42:01";
    Date date = null;
    try {
        date = sdf.parse(time);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    long time1 = date.getTime();
    System.out.println("时间毫秒数:" + time1);     
    //输出:时间毫秒数:1602384121000
}

5.Date转String(日期转日期格式字符串)

//Date转String(日期转日期格式字符串)
@Test
void dateTransformString()  {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date=new Date();
    String format = sdf.format(date);
    System.out.println("当前日期格式字符串:" + format);    
    //当前日期格式字符串:2022-05-02 16:41:52
    }

6.String转Date(日期格式字符串转日期)

//String转Date(日期格式字符串转日期)
@Test
void stringTransformDate()  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String string = "2020-10-14 10:10:00";
    Date date = null;
    try{
        date = sdf.parse(string);
    }catch (Exception e){
        e.printStackTrace();
    }
    System.out.println("日期:" + date);   
    //日期:Wed Oct 14 10:10:00 CST 2020
}

7.Long转Date(时间毫秒数转日期)

//Long转Date(时间毫秒数转日期)
@Test
void millisecondsTransformDate()  {
    long milliSecond = 1570650412089L;
    Date date = new Date();
    date.setTime(milliSecond);
    System.out.println("日期:"+ date);   
    //日期:Thu Oct 10 03:46:52 CST 2019
}

8.Date转Long(日期转为时间毫秒数)

//Date转Long(日期转为时间毫秒数)
@Test
void dateTransformMilliseconds()  {
    Date date = new Date();
    long time = date.getTime();
    System.out.println("时间毫秒数:" + time);     
    //输出:时间毫秒数:1651482816418
}

9.获取当前的开始时间

/**
* author:Yu Yang
* 获取当前的开始时间
*/
public static Date getDayBegin(){
   Calendar cal = new GregorianCalendar();
   cal.set(Calendar.HOUR_OF_DAY, 0);
   cal.set(Calendar.MINUTE, 0);
   cal.set(Calendar.SECOND, 0);
   cal.set(Calendar.MILLISECOND, 0);

   return cal.getTime();
}
结果展示
System.out.println(getDayBegin());  //Thu Jun 23 00:00:00 CST 2022

10.获取当前的结束时间

/**
* author:Yu Yang
* 获取当前的结束时间
*/
public static Date getDayEnd(){
   Calendar cal = new GregorianCalendar();
   cal.set(Calendar.HOUR_OF_DAY, 23);
   cal.set(Calendar.MINUTE, 59);
   cal.set(Calendar.SECOND, 59);

   return cal.getTime();
}
结果展示
System.out.println(getDayEnd());    //Thu Jun 23 23:59:59 CST 2022

11.获取昨天的开始时间

/**
 * author:Yu Yang
 * 获取昨天的开始时间
 */
public static Date getYestodayBegin(){
    Calendar cal = new GregorianCalendar();
    cal.setTime(getDayBegin());
    cal.add(Calendar.DAY_OF_MONTH, -1);
    return cal.getTime();
}
结果展示
System.out.println(getYestodayBegin()); //Wed Jun 22 00:00:00 CST 2022

12.获取昨天的结束时间

/**
* author:Yu Yang
 * 获取昨天的结束时间
 */
public static Date getYestodayEnd(){
    Calendar cal = new GregorianCalendar();
    cal.setTime(getDayEnd());
    cal.add(Calendar.DAY_OF_MONTH, -1);

    return cal.getTime();
}
结果展示
System.out.println(getYestodayEnd());   //Wed Jun 22 23:59:59 CST 2022

13.获取明天的开始时间

/**
*author:Yu Yang
* 获取明天的开始时间
*/
public static Date getTomorrowBegin(){
   Calendar cal = new GregorianCalendar();
   cal.setTime(getDayBegin());
   cal.add(Calendar.DATE, 1);
   return cal.getTime();
}
结果展示
 System.out.println(getTomorrowBegin()); //Fri Jun 24 00:00:00 CST 2022

14.获取明天的结束时间

/**
* author:Yu Yang
* 获取明天的结束时间
*/
public static Date getTomorrowEnd(){
   Calendar cal = new GregorianCalendar();
   cal.setTime(getDayEnd());
   cal.add(Calendar.DATE, 1);
   return cal.getTime();
}
结果展示
System.out.println(getTomorrowEnd());   //Fri Jun 24 23:59:59 CST 2022

15.获取指定日期的第二天同一时间

/**
* author:Yu Yang
* 获取指定日期的第二天同一时间
*/
public static Date getTSpecifyDateBegin(Date date){
   Calendar cal = new GregorianCalendar();
   cal.setTime(date);
   cal.add(Calendar.DAY_OF_MONTH, 1);
   return cal.getTime();
}
结果展示
System.out.println(getTSpecifyDateBegin(new Date()));   //Fri Jun 24 11:33:54 CST 2022

16.获取指定日期0时0分0秒

/**
 * author:Cui Yang
 * 获取指定日期0时0分0秒
 */
public static Date getDateBegin(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND,0);
    return calendar.getTime();
}
结果展示
System.out.println(getDateBegin(new Date()));   //Thu Jun 23 00:00:00 CST 2022

17.获取指定日期23时59分59秒

/**
* author:Cui Yang
* 获取指定日期23时59分59秒
*/
public static Date getDateEnded(Date date) {
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(date);
   calendar.set(Calendar.HOUR_OF_DAY, 23);
   calendar.set(Calendar.MINUTE, 59);
   calendar.set(Calendar.SECOND, 59);
   return calendar.getTime();
}
结果展示
System.out.println(getDateEnded(new Date()));   //Thu Jun 23 23:59:59 CST 2022

18.获取上个月的开始时间

/**
* author:Yu Yang
* 获取上个月的开始时间
*/
public static Date getBeforeFirstMonth(){

   Calendar calendar = Calendar.getInstance();// 获取当前日期
   calendar.add(Calendar.YEAR, 0);
   calendar.add(Calendar.MONTH, -1);
   calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
   calendar.set(Calendar.HOUR_OF_DAY, 0);
   calendar.set(Calendar.MINUTE, 0);
   calendar.set(Calendar.SECOND, 0);
   calendar.set(Calendar.MILLISECOND, 0);
   return calendar.getTime();
    }
结果展示
System.out.println(getBeforeFirstMonth());  //Sun May 01 00:00:00 CST 2022

19.获取上个月的结束时间

/**
* author:Yu Yang
* 获取上个月的结束时间
*/
public static Date getBeforeLastMonth(){
   Calendar calendar = Calendar.getInstance();// 获取当前日期
   calendar.add(Calendar.YEAR, 0);
   calendar.add(Calendar.MONTH, -1);
   calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
   calendar.set(Calendar.HOUR_OF_DAY, 23);
   calendar.set(Calendar.MINUTE, 59);
   calendar.set(Calendar.SECOND, 59);
   calendar.set(Calendar.MILLISECOND, 999);
   return calendar.getTime();
}
结果展示
System.out.println(getBeforeLastMonth());   //Tue May 31 23:59:59 CST 2022

20.获取指定日期,下个月的第一天

/**
 * 获取指定日期,下个月的第一天
 */
public static Date getNextMonthBegin(Date date){
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.YEAR, 0);
    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);// 获取当前月最后一天
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTime();
}
结果展示
System.out.println(getNextMonthBegin(new Date()));  //Fri Jul 01 00:00:00 CST 2022

21.获取间隔多少天的截至时间字符串

/**
* author:Yu Yang
* 获取间隔多少天的截至时间字符串
*/
public static String getDelDirDateStr(Integer interval){
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
   Calendar calendar = Calendar.getInstance();// 获取当前日期
   calendar.add(Calendar.DAY_OF_MONTH, 0 - interval);// 获取当前月最后一天
   return dateFormat.format(calendar.getTime());
}
结果展示
System.out.println(getDelDirDateStr(30));   //20220524

22.获取间隔多少天的截至时间date

/**
 * author:Yu Yang
 * 获取间隔多少天的截至时间date
 */
public static Date getDelDirDate(Integer interval){
    Calendar calendar = Calendar.getInstance();// 获取当前日期
    calendar.add(Calendar.DAY_OF_MONTH, 0 - interval);// 获取当前月最后一天
    return calendar.getTime();
}
结果展示
System.out.println(getDelDirDate(30));  //Tue May 24 11:33:54 CST 2022

23.获取两个日期之间的所有日期的开始时间集合

/**
* 获取两个日期之间的所有日期的开始时间集合
* 返回Date的List
*/
public static List<Date> getBetweenDates(Date start, Date end){
   List<Date> result = new ArrayList<>();
   Calendar tempStart = Calendar.getInstance();
   tempStart.setTime(start);
   tempStart.add(Calendar.DAY_OF_YEAR,1);

   Calendar tempEnd = Calendar.getInstance();
   tempEnd.setTime(end);

   while (tempStart.before(tempEnd)){
       result.add(tempStart.getTime());
       tempStart.add(Calendar.DAY_OF_YEAR, 1);
   }
   return result;
}
结果展示
//获取两个日期之间的所有日期的开始时间集合
Date beginDate = new Date();
beginDate.setTime(1652976000000L);  //2022-05-20 00:00:00
Date endDate = new Date();
endDate.setTime(1655654400000L);  //2022-06-20 00:00:00
System.out.println(getBetweenDates(beginDate, endDate));    //[Sat May 21 00:00:00 CST 2022, Sun May 22 00:00:00 CST 2022, Mon May 23 00:00:00 CST 2022, Tue May 24 00:00:00 CST 2022, Wed May 25 00:00:00 CST 2022, Thu May 26 00:00:00 CST 2022, Fri May 27 00:00:00 CST 2022, Sat May 28 00:00:00 CST 2022, Sun May 29 00:00:00 CST 2022, Mon May 30 00:00:00 CST 2022, Tue May 31 00:00:00 CST 2022, Wed Jun 01 00:00:00 CST 2022, Thu Jun 02 00:00:00 CST 2022, Fri Jun 03 00:00:00 CST 2022, Sat Jun 04 00:00:00 CST 2022, Sun Jun 05 00:00:00 CST 2022, Mon Jun 06 00:00:00 CST 2022, Tue Jun 07 00:00:00 CST 2022, Wed Jun 08 00:00:00 CST 2022, Thu Jun 09 00:00:00 CST 2022, Fri Jun 10 00:00:00 CST 2022, Sat Jun 11 00:00:00 CST 2022, Sun Jun 12 00:00:00 CST 2022, Mon Jun 13 00:00:00 CST 2022, Tue Jun 14 00:00:00 CST 2022, Wed Jun 15 00:00:00 CST 2022, Thu Jun 16 00:00:00 CST 2022, Fri Jun 17 00:00:00 CST 2022, Sat Jun 18 00:00:00 CST 2022, Sun Jun 19 00:00:00 CST 2022]

24.获取两个日期之间所有月份的集合

/**
* 获取两个日期之间所有月份的集合
*/
public static List<Date> getBetweenMonth(Date start, Date end){
   List<Date> result = new ArrayList<>();
   Calendar tempStart = Calendar.getInstance();
   tempStart.setTime(start);
   tempStart.set(tempStart.get(Calendar.YEAR), tempStart.get(Calendar.MONTH), 1);

   Calendar tempEnd = Calendar.getInstance();
   tempEnd.setTime(end);
   tempEnd.set(tempEnd.get(Calendar.YEAR), tempEnd.get(Calendar.MONTH), 2);

   while (tempStart.before(tempEnd)){
       result.add(tempStart.getTime());
       tempStart.add(Calendar.MONTH,1);
   }
   return result;
}
结果展示
System.out.println(getBetweenMonth(beginDate, endDate));    //[Sun May 01 00:00:00 CST 2022, Wed Jun 01 00:00:00 CST 2022]

25.判断当前时间是否在数据库起止时间范围内,是 ,返回true;否,返回false

主要用来判断类似这种时间,比如判断当前时间是否在12:00-15:00之间
/**
 * 判断当前时间是否在数据库起止时间范围内,是 ,返回true;否,返回false
 * @param dbBeginDate 开始日期
 * @param dbEndDate 结束日期
 * @return 结果
 */
public static boolean determineWhetherItMatchesTheTime(Date dbBeginDate, Date dbEndDate) {
    logger.info("-determineWhetherItMatchesTheTime-dbBeginDate:{},dbEndDate:{}", dbBeginDate, dbEndDate);
    boolean flag = false;
    // 获取当前日期和时间
    Date currentDate = new Date();
    // 将日期转换为LocalTime对象
    LocalTime localTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    logger.info("当前时间:{}", localTime);
    LocalTime dbBeginLocalTime = dbBeginDate.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    LocalTime dbEndLocalTime = dbEndDate.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    logger.info("dbBeginLocalTime:{},dbEndLocalTime:{}", dbBeginLocalTime, dbEndLocalTime);
    if (localTime.isBefore(dbEndLocalTime) && localTime.isAfter(dbBeginLocalTime)) {
        flag = true;
    }
    return flag;
}

26.获取2周前的时间字符串,打印输出20231205类似这种

/**
 * 获取2周前的时间字符串
 * @return 时间字符串
 */
public static String getDelDirDateStr(){
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    // 获取当前日期
    Calendar calendar = Calendar.getInstance();
    // 获取2周前的日期
    calendar.add(Calendar.WEEK_OF_YEAR, -2);
    return dateFormat.format(calendar.getTime());
}

27.传递日期格式及日期获取xx天前的时间字符串,打印输出20231205类似这种

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
getDelDirDateStrBydateFormatAndDays(dateFormat, Calender.DAT_OF_MONTH, -14);
/**
 *传递日期格式及日期获取xx天前的时间字符串
 * @param dateFormat 日期格式字符串
 * @param intervalType 日/周/年  这种的时间周期间隔
 * @param amount 量,比如天数
 * @return 时间字符串
 */
public static String getDelDirDateStrBydateFormatAndDays(SimpleDateFormat dateFormat, int intervalType, int amount){
    // 获取当前日期
    Calendar calendar = Calendar.getInstance();
    calendar.add(intervalType, amount);
    return dateFormat.format(calendar.getTime());
}

本人其他文章链接

1.java小工具util系列1:日期毫秒数转日期字符串
https://blog.csdn.net/a924382407/article/details/121955349

2.java小工具util系列2:获取字符modelStr在字符串str中第count次出现时的下标
https://blog.csdn.net/a924382407/article/details/121955455

3.java小工具util系列3:正则表达式匹配:匹配不包含@特殊字符的字符串
https://blog.csdn.net/a924382407/article/details/121955737

4.java小工具util系列4:String[] 转 List< Integer >
https://blog.csdn.net/a924382407/article/details/121956201

5.java小工具util系列5:基础工具代码(Msg、PageResult、Response、常量、枚举)
https://blog.csdn.net/a924382407/article/details/120952865

6.java小工具util系列6:java执行string返回boolean结果
https://blog.csdn.net/a924382407/article/details/117124536

7.java小工具util系列7:集合中实体对象转换 list中Enrey转Dto
https://blog.csdn.net/a924382407/article/details/121957545

8.java小工具util系列8:JSONObject获取key
https://blog.csdn.net/a924382407/article/details/121957607

9.java小工具util系列9:检测一个字符串是否是时间格式
https://blog.csdn.net/a924382407/article/details/123948881

10.java小工具util系列10:时间毫秒数、时间格式字符串、日期之间相互转化
https://blog.csdn.net/a924382407/article/details/124581851

![上传中...]()


刘大猫
6 声望1 粉丝

如果有天突然发现路的尽头还是路的话,希望你还没错过太多沿路的风景和眼前珍惜的人。