日期时间DateUtil-java工具类

本文介绍了一个实用的Java日期时间工具类,提供了多种日期时间格式化方法,包括获取当前日期时间、日期加减天数及计算两个日期间天数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

日期时间DateUtil-java工具类

/**
*日期时间-工具类
**/
public class DateUtil{
       //获取当前日期时间"yyyy-MM-dd HH:mm:ss"
       public static String getNowDateTimeString() {
             Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            return dateString;
       }

       //获取当前日期"yyyy-MM-dd"
       public static String getNowDateString() {
             Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(currentTime);
            return dateString;
       }

       // 获取当前时间 "yyyy-MM-dd"字符串
        public static String getDate(Date date) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//
            return dateFormat.format(date);
        }
       
       // 获取当前时间 "yyyy-MM-dd HH:mm:ss"字符串
        public static String getNowDate(Date date) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
            return dateFormat.format(date);
        }
     
       //当前日期date加减天数
       public static String addDay(Date date, int num) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, num);
            return DateUtil.getFormat(calendar.getTime());
      }
     
     //得到两个日期之间的天数
      public static Long getIntervelDays(Date dateBegin ,Date dateEnd) {
           if(dateBegin.compareTo(dateEnd) == 1)
                 return (dateBegin.getTime() - dateEnd.getTime()) / (3600 * 24 * 1000);
           else
                 return (dateEnd.getTime() - dateBegin.getTime()) / (3600 * 24 * 1000);
     }

    //Calendar 取得当前时间的方法 
     public static void main(String[] args) {  
    // 字符串转换日期格式  
    // DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    // 接收传入参数  
    // String strDate = args[1];  
    // 得到日期格式对象  
    // Date date = fmtDateTime.parse(strDate);  
 
    // 完整显示今天日期时间  
    String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());  
    System.out.println(str);  
 
    // 创建 Calendar 对象  
    Calendar calendar = Calendar.getInstance();  
 
    try  
    {  
      // 对 calendar 设置时间的方法  
      // 设置传入的时间格式  
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");  
      // 指定一个日期  
      Date date = dateFormat.parse("2013-6-1 13:24:16");  
      // 对 calendar 设置为 date 所定的日期  
      calendar.setTime(date);  
 
      // 按特定格式显示刚设置的时间  
      str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
      System.out.println(str);  
    }  
    catch (ParseException e)  
    {  
      e.printStackTrace();  
    }  
 
    // 或者另一種設置 calendar 方式  
    // 分別爲 year, month, date, hourOfDay, minute, second  
    calendar = Calendar.getInstance();  
    calendar.set(2013, 1, 2, 17, 35, 44);  
    str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
    System.out.println(str);  
 
    // Calendar 取得当前时间的方法  
    // 初始化 (重置) Calendar 对象  
    calendar = Calendar.getInstance();  
    // 或者用 Date 来初始化 Calendar 对象  
    calendar.setTime(new Date());  
 
    // setTime 类似上面一行  
    // Date date = new Date();  
    // calendar.setTime(date);  
 
    str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
    System.out.println(str);  
 
    // 显示年份  
    int year = calendar.get(Calendar.YEAR);  
    System.out.println("year is = " + String.valueOf(year));  
 
    // 显示月份 (从0开始, 实际显示要加一)  
    int month = calendar.get(Calendar.MONTH);  
    System.out.println("nth is = " + (month + 1));  
 
    // 本周几  
    int week = calendar.get(Calendar.DAY_OF_WEEK);  
    System.out.println("week is = " + week);  
 
    // 今年的第 N 天  
    int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);  
    System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);  
 
    // 本月第 N 天  
    int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);  
    System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));  
 
    // 3小时以后  
    calendar.add(Calendar.HOUR_OF_DAY, 3);  
    int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);  
    System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);  
 
    // 当前分钟数  
    int MINUTE = calendar.get(Calendar.MINUTE);  
    System.out.println("MINUTE = " + MINUTE);  
 
    // 15 分钟以后  
    calendar.add(Calendar.MINUTE, 15);  
    MINUTE = calendar.get(Calendar.MINUTE);  
    System.out.println("MINUTE + 15 = " + MINUTE);  
 
    // 30分钟前  
    calendar.add(Calendar.MINUTE, -30);  
    MINUTE = calendar.get(Calendar.MINUTE);  
    System.out.println("MINUTE - 30 = " + MINUTE);  
 
    // 格式化显示  
    str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
    System.out.println(str);  
 
    // 重置 Calendar 显示当前时间  
    calendar.setTime(new Date());  
    str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
    System.out.println(str);  
 
    // 创建一个 Calendar 用于比较时间  
    Calendar calendarNew = Calendar.getInstance();  
 
    // 设定为 5 小时以前,后者大,显示 -1  
    calendarNew.add(Calendar.HOUR, -5);  
    System.out.println("时间比较:" + calendarNew.compareTo(calendar));  
 
    // 设定7小时以后,前者大,显示 1  
    calendarNew.add(Calendar.HOUR, +7);  
    System.out.println("时间比较:" + calendarNew.compareTo(calendar));  
 
    // 退回 2 小时,时间相同,显示 0  
    calendarNew.add(Calendar.HOUR, -2);  
    System.out.println("时间比较:" + calendarNew.compareTo(calendar));  
   } 

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值