使用 DurationFormatUtils 格式化时间间隔毫秒值

原文链接:https://blog.youkuaiyun.com/KimSoft/article/details/5106984


  // 当前时间
  Date now = new Date();

  // 2010上海世博会开幕时间
  Date expo2010BeginDate = DateUtils.parseDate("2010-05-01 23:59:59", new String[] { "yyyy-MM-dd HH:mm:ss" });

  // 2010上海世博会闭幕时间
  Date expo2010EndDate = DateUtils.parseDate("2010-10-31 23:59:59", new String[] { "yyyy-MM-dd HH:mm:ss" });

  System.out.println(DurationFormatUtils.formatDuration(expo2010BeginDate.getTime() - now.getTime(), "d"));

  System.out.println(DurationFormatUtils.formatDuration(expo2010BeginDate.getTime() - expo2010EndDate.getTime(),
    "d"));

Duration formatting utilities and constants. The following table describes the tokens used in the pattern language for formatting.

以下是format参数

 

characterduration element
yyears
Mmonths
ddays
Hhours
mminutes
sseconds
Smilliseconds

 

    /**
     * 将时间间隔毫秒值转化为**分**秒**毫秒
     * @param intervalTime 
     * @return
     */
    public static String convertMillisecondsToMSS(Long intervalTime) {
        String str=DurationFormatUtils.formatDuration(intervalTime, "mm分ss秒SSS毫秒");
        return str;
    }
    
    /**
     * 将时间间隔毫秒值转化为**分**秒
     * @param intervalTime 
     * @return
     */
    public static String convertMillisecondsToMS(Long intervalTime) {
        String str=DurationFormatUtils.formatDuration(intervalTime, "mm分ss秒");
        return str;
    }
    
    /**
     * 将时间间隔毫秒值转化为**时**分
     * @param intervalTime 
     * @return
     */
    public static String convertMillisecondsToHM(Long intervalTime) {
        String str = DurationFormatUtils.formatDuration(intervalTime, "HH小时mm分");
        return str;
    }
    
    /**
     * 将时间间隔毫秒值转化为**天
     * @param intervalTime 
     * @return
     */
    public static String convertMillisecondsToDD(Long intervalTime) {
        String str = DurationFormatUtils.formatDuration(intervalTime, "dd天");
        return str;
    }    
    
    /**
     * 拿到1天后的时间点
     * @param date
     * @return
     */
    public static Date getNextDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }
    
    /**
     * 拿到n天以后的时间点
     * @param date
     * @param number
     * @return
     */
    public static Date getNextExpiringDate(Date date,int number){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, number);
        return calendar.getTime();
    }
    
    /**
     * 字符串转时间
     * @param source
     * @return
     */
    public static Date StringToDate(String source){
        if(null==source || source.equals(""))
            return null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 字符串转时间
     * @param source
     * @param format
     * @return
     */
    public static Date StringToDate(String source,String format){
        if(null==source || source.equals(""))
            return null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 时间转字符串
     * @param date
     * @return
     */
    public static String dateToString(Date date){
        if(null==date)
            return "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    /**
     * 拿到yyyy-MM-dd中的dd
     * @param date
     * @return
     */
    public static int getCurrentDayNumber(Date date){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

 

总结下Java Calendar的使用:

参考资料:https://www.cnblogs.com/lsy-lancen/p/6122634.html

The java.util.calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.Following are the important points about Calendar

API 文档上给的官方解释:

java.util.Calendar类是一个抽象类,在某一特定的瞬间或日历上,提供年、月、日、小时之间的转换提供方法。

只要来眼界下Calendar中的常量(filed)的作用

1

2

3

4

5

6

7

8

9

10

11

12

13

         Calendar cal = Calendar.getInstance();

         

         cal.get(Calendar.DATE);//-----------------------当天 1-31

         cal.get(Calendar.DAY_OF_MONTH);//---------------当天 1-31

         cal.get(Calendar.DAY_OF_WEEK);//----------------从星期天开始计算,如果今天星期二,那么返回3

         cal.get(Calendar.DAY_OF_YEAR);//----------------

         cal.get(Calendar.HOUR);//-----------------------12小时制

         cal.get(Calendar.HOUR_OF_DAY);//----------------24小时制,一般使用这个属性赋值

         cal.get(Calendar.MILLISECOND);//----------------

         cal.get(Calendar.MINUTE);//---------------------

         cal.get(Calendar.SECOND);//---------------------

         cal.get(Calendar.WEEK_OF_MONTH);//--------------

         cal.get(Calendar.WEEK_OF_YEAR);//---------------

         cal.get(Calendar.MONTH);//-----------------------月份获取需要 +1,那么,赋值时需要 -1

 1.运用这些常量赋值,通过这些常量获取值,同样可以通过它进行赋值

2.赋值时,week与mooth是需要注意,week需要制定setFirstDayOfWeek,月份则需要加减1

3.赋值时,一般采用年、月、日、时、分、秒

  Calendar.YEAR 、Calendar.MONTH 、Calendar.DAY_OF_MONTH、 Calendar.HOUR_OF_DAY 、Calendar.MINUTE、 Calendar.SECON 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值