• 使用joda-time工具类 计算时间相差多少 天,小时,分钟,秒


    使用joda-time工具类 计算时间相差多少 天,小时,分钟,秒

     

    下面程序使用了两种方法计算两个时间相差 天,小时,分钟,秒
     1 package jodotest;
     2 
     3 import java.text.ParseException;
     4 import java.text.SimpleDateFormat;
     5 import java.util.Date;
     6 
     7 import org.joda.time.DateTime;
     8 import org.joda.time.Days;
     9 import org.joda.time.Hours;
    10 import org.joda.time.Interval;
    11 import org.joda.time.Minutes;
    12 import org.joda.time.Period;
    13 import org.joda.time.Seconds;
    14 
    15 /**
    16 * @author wanggang
    17 * 
    18 */
    19 public class Test {
    20 public static void test1(Date d1, Date d2){
    21 
    22 // 毫秒ms
    23 long diff = d2.getTime() - d1.getTime();
    24 
    25 long diffSeconds = diff / 1000 % 60;
    26 long diffMinutes = diff / (60 * 1000) % 60;
    27 long diffHours = diff / (60 * 60 * 1000) % 24;
    28 long diffDays = diff / (24 * 60 * 60 * 1000);
    29 
    30 System.out.print("时间相差:");
    31 System.out.print(diffDays + " 天 ");
    32 System.out.print(diffHours + " 小时 ");
    33 System.out.print(diffMinutes + " 分钟 ");
    34 System.out.print(diffSeconds + " 秒.");
    35 System.out.println();
    36 }
    37 
    38 public static void test2(Date d1, Date d2) throws ParseException{
    39 
    40 DateTime dt1 = new DateTime(d1);
    41 DateTime dt2 = new DateTime(d2);
    42 System.out.print("时间相差:");
    43 System.out.print(Days.daysBetween(dt1, dt2).getDays() + " 天 ");
    44 System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " 小时 ");
    45 System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " 分钟 ");
    46 System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60+ " 秒.");
    47 System.out.println();
    48 }
    49 
    50 public static void test3(Date d1, Date d2){
    51 Interval interval = new Interval(d1.getTime(), d2.getTime());
    52 Period p = interval.toPeriod();
    53 System.out.println("时间相差:"+p.getDays()+" 天 " + p.getHours()+ " 小时 "+p.getMinutes()+" 分钟"+p.getSeconds()+" 秒");
    54 }
    55 
    56 
    57 
    58 
    59 /**
    60 * @param args
    61 * @throws ParseException 
    62 */
    63 public static void main(String[] args) throws ParseException {
    64 String dateStart = "2013-08-13 16:29:58";
    65 String dateStop = "2013-08-13 16:31:48";
    66 
    67 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    68 
    69 Date d1 = format.parse(dateStart);
    70 Date d2 = format.parse(dateStop);
    71 test1(d1, d2);
    72 test2(d1, d2);
    73 test3(d1, d2);
    74 }
    75 }
    程序输出:
    时间相差:0 天 0 小时 1 分钟 50 秒.
    时间相差:0 天 0 小时 1 分钟 50 秒.
    时间相差:0 天 0 小时 1 分钟50 秒
    ateTime myBirthDate =
                            new DateTime(2020, 2, 1, 12, 35, 0, 0);
                    DateTime now =
                            new DateTime(2020, 1, 17, 0, 0, 0, 0);
                    Period period = new Period(myBirthDate, now);
                    PeriodFormatter formatter = new PeriodFormatterBuilder()
                            .appendYears().appendSuffix("")
                            .appendMonths().appendSuffix("")
                            .appendWeeks().appendSuffix("")
                            .appendDays().appendSuffix("")
                            .appendHours().appendSuffix("小时")
                            .appendMinutes().appendSuffix("")
                            .appendSeconds().appendSuffix("")
                            .printZeroNever()
                            .toFormatter();
                    String elapsed = formatter.print(period);
                    System.out.println(elapsed); //-Joda-Time 计算两个时间差(年,月,星期,日,小时,分钟,秒,毫秒)
    原创逐墨飞扬 最后发布于2019-12-01 09:54:27 阅读数 142  收藏
    展开
    计算方法
    import org.joda.time.DateTime;
    import org.joda.time.Interval;
    import org.joda.time.Period;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    
    public class DiffDateTime {
        /**
         * Joda-Time 计算两个时间差(年,月,星期,日,小时,分钟,秒,毫秒)   注: 开始时间 和 结束时间 格式须相同
         * @param startDateTime     开始时间
         * @param endDateTime       结束时间
         * @param dateTimeType      时间格式(2018年01月20日 21:02:37(yyyy年MM月dd日 HH:mm:ss))
         */
        public static void calculateTimeDifference(String startDateTime, String endDateTime, String dateTimeType) {
    
            DateTimeFormatter format = DateTimeFormat.forPattern(dateTimeType);
            DateTime dateTimeStart = format.parseDateTime(startDateTime);
            DateTime dateTimeEnd = format.parseDateTime(endDateTime);
    
            if (dateTimeStart.isAfter(dateTimeEnd)) {
                DateTime temp = dateTimeStart;
                dateTimeStart = dateTimeEnd;
                dateTimeEnd = temp;
            }
    
            Interval interval = new Interval(dateTimeStart.getMillis(), dateTimeEnd.getMillis());
            Period p = interval.toPeriod();
            System.out.println(p.toString());
            System.out.printf("两个日期的时间差:%d 年 %d 个月 %d 星期 %d 天 %d 小时 %d 分钟 %d 秒 %d 毫秒
    ",
                    p.getYears(), p.getMonths(), p.getWeeks(), p.getDays(), p.getHours(), p.getMinutes(), p.getSeconds(), p.getMillis());
    
        }
    }
    
    注: 开始时间 和 结束时间 格式 必须相同
    
    使用方式
    public static void main(String[] args) {
            calculateTimeDifference("2018年01月20日 21:02:37", "2019年03月01日 13:24:46", "yyyy年MM月dd日 HH:mm:ss");
            calculateTimeDifference("2018年01月27日", "2019年03月07日", "yyyy年MM月dd日");
            calculateTimeDifference("2019年03月01日", "2018年01月20日", "yyyy年MM月dd日");
        }
    
    输出结果
    P1Y1M1W1DT16H22M9S
    两个日期的时间差:11 个月 1 星期 116 小时 22 分钟 90 毫秒
    P1Y1M1W1D
    两个日期的时间差:11 个月 1 星期 10 小时 0 分钟 00 毫秒
    P1Y1M1W2D
    两个日期的时间差:11 个月 1 星期 20 小时 0 分钟 00 毫秒
    
    
    ————————————————
    版权声明:本文为CSDN博主「逐墨飞扬」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/ZXY_9264_ANG/article/details/103332374

      药械盘点

    String spent = "";
                if(ele.getDBegin() != null && ele.getDEnd() != null)
                {
                    try
                    {
                        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
                        DateTime dateTimeStart = format.parseDateTime(simpleDateFormat.format(ele.getDBegin()));
                        DateTime dateTimeEnd = format.parseDateTime(simpleDateFormat.format(ele.getDEnd()));
                        Period period = new Period(dateTimeStart, dateTimeEnd);
                        PeriodFormatter formatter = new PeriodFormatterBuilder()
                                .appendYears().appendSuffix("年")
                                .appendMonths().appendSuffix("月")
                                .appendWeeks().appendSuffix("星期")
                                .appendDays().appendSuffix("天")
                                .appendHours().appendSuffix("小时")
                                .appendMinutes().appendSuffix("分")
                                .appendSeconds().appendSuffix("秒")
                                .printZeroNever()
                                .toFormatter();
                        spent = formatter.print(period);
                    }
                    catch (Exception ex)
                    {
                        Log.e("error", ex.getMessage());
                    }
                }
  • 相关阅读:
    12306抢票系统——ER图及数据表
    深度学习攻防对抗(JCAI-19 阿里巴巴人工智能对抗算法竞赛)
    用Tensorflow实现DCGAN
    机器学习实战:数据预处理之独热编码(One-Hot Encoding)
    K最近邻算法
    正则表达式模块re
    2013百度研发笔试
    python初准备:安装easy_install和pip
    网络设备作用和工作ISO层
    01背包初始化的理解
  • 原文地址:https://www.cnblogs.com/ioriwellings/p/12320832.html
Copyright © 2020-2023  润新知