java时间处理--持续时间格式化工具和常量类DurationFormatUtils

 

阅读目录

1.DurationFormatUtils类简介

1.1描述

 1 /**
 2  * <p>Duration formatting utilities and constants. The following table describes the tokens 
 3  * used in the pattern language for formatting. </p>
 4  * <table border="1">
 5  *  <tr><th>character</th><th>duration element</th></tr>
 6  *  <tr><td>y</td><td>years</td></tr>
 7  *  <tr><td>M</td><td>months</td></tr>
 8  *  <tr><td>d</td><td>days</td></tr>
 9  *  <tr><td>H</td><td>hours</td></tr>
10  *  <tr><td>m</td><td>minutes</td></tr>
11  *  <tr><td>s</td><td>seconds</td></tr>
12  *  <tr><td>S</td><td>milliseconds</td></tr>
13  * </table>
14  *
15  * @since 2.1
16  * @version $Id: DurationFormatUtils.java 1144993 2011-07-11 00:51:16Z ggregory $
17  */
18 public class DurationFormatUtils {...}

以上是DurationFormatUtils类源码中的描述,从中可见DurationFormatUtils是持续时间格式化工具和常量类。在格式化过程中使用的pattern如下

字符所代表含义
y
M
d
H
小时
m
分钟
 
s
 秒
 
S
 毫秒

 

 

 

 

 

 

 

 

 

 

 

 

1.2maven地址

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

 

2.方法

回到顶部

2.1构造方法 DurationFormatUtils()

     /**
      * <p>在常规编程中DurationFormatUtils不应该被实例化.</p>      
    * <p>这个public的构造方法是为了给其他工具的集成准备的.</p>
*/ public DurationFormatUtils() { super(); }

2.2 formatDurationHMS(long durationMillis)

    /**
     * <p>Formats the time gap as a string.</p>
     * 
     * <p>The format used is ISO8601-like:
     * <i>H</i>:<i>m</i>:<i>s</i>.<i>S</i>.</p>
     * 
     * @param durationMillis  the duration to format
     * @return the formatted duration, not null
     */
    public static String formatDurationHMS(long durationMillis) {
        return formatDuration(durationMillis, "H:mm:ss.SSS");
    }

2.2.1方法描述:

  将时间间隔格式化为字符串,参数列表是一个long类型的毫秒数,格式化后的结果格式为H:mm:ss.SSS 时:分:秒.毫秒

2.2.2方法测试:

 public static void testFormatDurationHMS(){
    long timeGap = 360000;//36000毫秒
    String formatResult = DurationFormatUtils.formatDurationHMS(timeGap);
   System.out.println(formatResult);
 }

2.2.3运行结果:

0:06:00.000

回到顶部

 2.3 formatDurationISO(long durationMillis)

    /**
     * <p>Formats the time gap as a string.</p>
     * 
     * <p>The format used is the ISO8601 period format.</p>
     * 
     * <p>This method formats durations using the days and lower fields of the
     * ISO format pattern, such as P7D6TH5M4.321S.</p>
     * 
     * @param durationMillis  the duration to format
     * @return the formatted duration, not null
     */
    public static String formatDurationISO(long durationMillis) {
        return formatDuration(durationMillis, ISO_EXTENDED_FORMAT_PATTERN, false);
    }

2.3.1方法描述

  将时间间隔格式化为字符串,参数列表是一个long类型的毫秒数,格式化所使用的格式为ISO8601

2.3.2方法测试

public static void testFormatDurationISO(){
   long timeGap = 360000;//36000毫秒
   String formatResult = DurationFormatUtils.formatDurationISO(timeGap);
  System.out.println(formatResult);
}

2.3.3运行结果

P0Y0M0DT0H6M0.000S

回到顶部

2.4 formatDuration(long durationMillis, String format)

 /**
     * <p>格式化时间间隔为字符串,使用指定的格式,并且自动填充0,使用默认的时区</p>
     * <p>此方法格式化时间间隔只能格式化为天或者更低级别(时,分,秒...),月份或者更高级别的不可用</p>
     * @param 毫秒数
     * @param 格式化后的格式
     * @return the formatted duration, not null
     */
    public static String formatDuration(long durationMillis, String format) {
        return formatDuration(durationMillis, format, true);
    }
/** * 方法重载,多了个参数boolean padWithZeros * 为true,则会在格式化前自动加0 例如:格式化格式为dd,格式化后为1天,则自动填充为01天,否则不会自动填充 */ public static String formatDuration(long durationMillis, String format, boolean padWithZeros) {...}

 

2.4.1方法测试

public static void testFormatDuration(){
   long timeGap = 3600000000l;
   String dayFormatPattern = "当前时间段月数:ddd天";
   System.out.println(DurationFormatUtils.formatDuration(timeGap,dayFormatPattern));
   System.out.println(DurationFormatUtils.formatDuration(timeGap,dayFormatPattern,false));
   String monthFormatPattern = "当前时间段月数:M月";
  System.out.println(DurationFormatUtils.formatDuration(timeGap,monthFormatPattern));
}

2.4.2运行结果

当前时间段月数:041天
当前时间段月数:41天
当前时间段月数:0月

回到顶部

 2.5 formatDurationWords(long durationMillis,boolean suppressLeadingZeroElements,boolean suppressTrailingZeroElements) 

/**
     * <p>将时间间隔格式化为字符串.</p>
     * 
     * <p>只适用于天或者以下</p>
     * 
     * @param durationMillis  时间段,毫秒
     * @param suppressLeadingZeroElements  是否禁止结果前加0(,英文不行,翻译不准,看运行结果)
     * @param suppressTrailingZeroElements  是否禁止在结果后加0(看运行结果)
     * @return the formatted text in days/hours/minutes/seconds, not null
     */
    public static String formatDurationWords(
        long durationMillis,
        boolean suppressLeadingZeroElements,
        boolean suppressTrailingZeroElements) {}

2.5.1测试

  public static void testFormatDurationWords(){
        long timeGap = 360000l;
        System.out.println(DurationFormatUtils.formatDurationWords(timeGap,true,true));
        System.out.println(DurationFormatUtils.formatDurationWords(timeGap,false,true));
        long timeGap2 = 360300l;
        System.out.println(DurationFormatUtils.formatDurationWords(timeGap2,false,true));
        System.out.println(DurationFormatUtils.formatDurationWords(timeGap2,false,false));
    }

2.5.2运行结果

//整体结果为 : 0 days 0 hours 6 minutes 0 seconds
6 minutes                             //前禁止,后禁止
0 days 0 hours 6 minutes              //前不禁止,后禁止
0 days 0 hours 6 minutes              //前不禁止,后禁止
0 days 0 hours 6 minutes 0 seconds    //前不禁止,后不禁止            

 回到顶部

2.6 formatPeriod(long startMillis, long endMillis, String format, boolean padWithZeros, TimeZone timezone)

  /**
     * <p>使用指定的pattern格式化时间间隔.
     * 可选择是否自动在数值前填充0
     * timezone 可选. </p>
     * @param startMillis  开始时间毫秒
     * @param endMillis  结束时间毫秒
     * @param format  格式化pattern
     * @param padWithZeros  是否填充0
     * @param timezone  时区
     * @return the formatted duration, not null
     */
    public static String formatPeriod(long startMillis, long endMillis, String format, boolean padWithZeros, TimeZone timezone) {...}

2.6.1方法测试

  public static void testFormatPeriod(){
        long timeGap1 = 0l;
        long timeGap2 = 31536001000l;//一年一秒
        String formatPattern = "yyyy:MM:dd HH:mm:ss";
        System.out.println(DurationFormatUtils.formatPeriod(timeGap1,timeGap2,formatPattern,false,TimeZone.getDefault()));
        System.out.println(DurationFormatUtils.formatPeriod(timeGap1,timeGap2,formatPattern,true,TimeZone.getDefault()));
    }

2.6.2运行结果

1:0:0 0:0:1
0001:00:00 00:00:01

2.6.3重载||内部调用

  public static String formatPeriod(long startMillis, long endMillis, String format) {
        return formatPeriod(startMillis, endMillis, format, true, TimeZone.getDefault());
    }

  public static String formatPeriodISO(long startMillis, long endMillis) {
        return formatPeriod(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault());
    }

 

转载于:https://www.cnblogs.com/carryLess/p/9198681.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据工具,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的: META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet.class org.apache.commons.lang.CharSetUtils.class org.apache.commons.lang.CharUtils.class org.apache.commons.lang.ClassUtils.class org.apache.commons.lang.Entities$ArrayEntityMap.class org.apache.commons.lang.Entities$BinaryEntityMap.class org.apache.commons.lang.Entities$EntityMap.class org.apache.commons.lang.Entities$HashEntityMap.class org.apache.commons.lang.Entities$LookupEntityMap.class org.apache.commons.lang.Entities$MapIntMap.class org.apache.commons.lang.Entities$PrimitiveEntityMap.class org.apache.commons.lang.Entities$TreeEntityMap.class org.apache.commons.lang.Entities.class org.apache.commons.lang.IllegalClassException.class org.apache.commons.lang.IncompleteArgumentException.class org.apache.commons.lang.IntHashMap$Entry.class org.apache.commons.lang.IntHashMap.class org.apache.commons.lang.LocaleUtils.class org.apache.commons.lang.NotImplementedException.class org.apache.commons.lang.NullArgumentException.class org.apache.commons.lang.NumberRange.class org.apache.commons.lang.NumberUtils.class org.apache.commons.lang.ObjectUtils$Null.class org.apache.commons.lang.ObjectUtils.class org.apache.commons.lang.RandomStringUtils.class org.apache.commons.lang.SerializationException.class org.apache.commons.lang.SerializationUtils.class org.apache.commons.lang.StringEscapeUtils.class org.apache.commons.lang.StringUtils.class org.apache.commons.lang.SystemUtils.class org.apache.commons.lang.UnhandledException.class org.apache.commons.lang.Validate.class org.apache.commons.lang.WordUtils.class org.apache.commons.lang.builder.CompareToBuilder.class org.apache.commons.lang.builder.EqualsBuilder.class org.apache.commons.lang.builder.HashCodeBuilder.class org.apache.commons.lang.builder.ReflectionToStringBuilder$1.class org.apache.commons.lang.builder.ReflectionToStringBuilder.class org.apache.commons.lang.builder.StandardToStringStyle.class org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$NoFieldNameToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$ShortPrefixToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$SimpleToStringStyle.class org.apache.commons.lang.builder.ToStringStyle.class org.apache.commons.lang.enum.Enum$Entry.class org.apache.commons.lang.enum.Enum.class org.apache.commons.lang.enum.EnumUtils.class org.apache.commons.lang.enum.ValuedEnum.class org.apache.commons.lang.enums.Enum$Entry.class org.apache.commons.lang.enums.Enum.class org.apache.commons.lang.enums.EnumUtils.class org.apache.commons.lang.enums.ValuedEnum.class org.apache.commons.lang.exception.ExceptionUtils.class org.apache.commons.lang.exception.Nestable.class org.apache.commons.lang.exception.NestableDelegate.class org.apache.commons.lang.exception.NestableError.class org.apache.commons.lang.exception.NestableException.class org.apache.commons.lang.exception.NestableRuntimeException.class org.apache.commons.lang.math.DoubleRange.class org.apache.commons.lang.math.FloatRange.class org.apache.commons.lang.math.Fraction.class org.apache.commons.lang.math.IntRange.class org.apache.commons.lang.math.JVMRandom.class org.apache.commons.lang.math.LongRange.class org.apache.commons.lang.math.NumberRange.class org.apache.commons.lang.math.NumberUtils.class org.apache.commons.lang.math.RandomUtils.class org.apache.commons.lang.math.Range.class org.apache.commons.lang.mutable.Mutable.class org.apache.commons.lang.mutable.MutableBoolean.class org.apache.commons.lang.mutable.MutableByte.class org.apache.commons.lang.mutable.MutableDouble.class org.apache.commons.lang.mutable.MutableFloat.class org.apache.commons.lang.mutable.MutableInt.class org.apache.commons.lang.mutable.MutableLong.class org.apache.commons.lang.mutable.MutableObject.class org.apache.commons.lang.mutable.MutableShort.class org.apache.commons.lang.text.CompositeFormat.class org.apache.commons.lang.text.StrBuilder$StrBuilderReader.class org.apache.commons.lang.text.StrBuilder$StrBuilderTokenizer.class org.apache.commons.lang.text.StrBuilder$StrBuilderWriter.class org.apache.commons.lang.text.StrBuilder.class org.apache.commons.lang.text.StrLookup$MapStrLookup.class org.apache.commons.lang.text.StrLookup.class org.apache.commons.lang.text.StrMatcher$CharMatcher.class org.apache.commons.lang.text.StrMatcher$CharSetMatcher.class org.apache.commons.lang.text.StrMatcher$NoMatcher.class org.apache.commons.lang.text.StrMatcher$StringMatcher.class org.apache.commons.lang.text.StrMatcher$TrimMatcher.class org.apache.commons.lang.text.StrMatcher.class org.apache.commons.lang.text.StrSubstitutor.class org.apache.commons.lang.text.StrTokenizer.class org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time.DurationFormatUtils$Token.class org.apache.commons.lang.time.DurationFormatUtils.class org.apache.commons.lang.time.FastDateFormat$CharacterLiteral.class org.apache.commons.lang.time.FastDateFormat$NumberRule.class org.apache.commons.lang.time.FastDateFormat$PaddedNumberField.class org.apache.commons.lang.time.FastDateFormat$Pair.class org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons.lang.time.FastDateFormat$TimeZoneDisplayKey.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNameRule.class org.apache.commons.lang.time.FastDateFormat$TimeZoneNumberRule.class org.apache.commons.lang.time.FastDateFormat$TwelveHourField.class org.apache.commons.lang.time.FastDateFormat$TwentyFourHourField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitMonthField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitNumberField.class org.apache.commons.lang.time.FastDateFormat$TwoDigitYearField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedMonthField.class org.apache.commons.lang.time.FastDateFormat$UnpaddedNumberField.class org.apache.commons.lang.time.FastDateFormat.class org.apache.commons.lang.time.StopWatch.class

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值