java- 2 个不同日期中 2 个不同时间之间的差异

java- difference between 2 different times in 2 different days

我想写一个程序,显示从现在到未来某个特定时间的剩余时间。因为我想使用 Joda-Time,所以我搜索了很多并找到了一些代码(例如 this one),但我不知道如何在 2 个不同的日子里对 2 个不同的时间执行此操作。有什么想法吗?

package com.test;

import java.util.Calendar;

import org.joda.time.Duration;

public class Main {

public Main() {
}

public static void main(String[] args) {
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();

    calendar1.add(Calendar.DAY_OF_YEAR, 1);
    calendar1.add(Calendar.HOUR_OF_DAY, 2);
    calendar1.add(Calendar.MINUTE, 10);

    Duration duration = new Duration(calendar2.getTimeInMillis(), calendar1.getTimeInMillis());
    System.out.println(duration.getMillis());
    System.out.println(duration.getStandardDays());
    System.out.println(duration.getStandardMinutes());
    System.out.println(duration.getStandardHours());
    System.out.println(duration.getStandardSeconds());
}

}

我认为这可能对您有所帮助。

此代码将提供完全相同的持续时间。您需要将 calendar2 设置为将来的固定日期才能获得您想要的结果。

更新 1

你可以像这样为将来的一个固定日期准备。可以有其他更好的方法来实现同样的目标。

package com.test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.joda.time.Duration;

public class Main {

public Main() {
}

public static void main(String[] args) throws ParseException {
    Calendar calendar1 = Calendar.getInstance();

    DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    Date date1 = calendar1.getTime();
    Date date2 = df.parse("2015/03/20 10:30:00");


    Duration duration = new Duration(date1.getTime(), date2.getTime());
    long millis = duration.getMillis();
    long days = duration.getStandardDays();
    long mins = duration.getStandardMinutes();
    long hrs = duration.getStandardHours();
    long secs = duration.getStandardSeconds();

    System.out.println(days + ":" + (hrs % 24) + ":" + (mins%60) + ":" + (secs%(mins)) + " remaining");
}

}

你的意思是……

DateTime from = new DateTime(2014, DateTimeConstants.FEBRUARY, 15, 8, 51, 30, 100);
DateTime to = new DateTime(2016, DateTimeConstants.DECEMBER, 25, 17, 01, 51, 50);

Interval i = new Interval(from, to);
Period p = i.toPeriod(PeriodType.yearMonthDayTime());

System.out.println(p.getYears() + " years");
System.out.println(p.getMonths() + " months");
System.out.println(p.getDays() + " days");
System.out.println(p.getHours() + " hours");
System.out.println(p.getSeconds() + " seconds");
System.out.println(p.getMillis() + " millis");

输出

2 years
10 months
10 days
8 hours
20 seconds
950 millis

正在使用...

DateTime from = new DateTime();
DateTime to = new DateTime(2015, 3, 21, 2, 15, 10);

而是打印...

0 years
0 months
7 days
15 hours
26 seconds
755 millis

您提到您想要使用 Joda-time,因此其他解决方案之一可能更适合您。

但是,现在也可以使用 Java 8 的本地时间库轻松完成相同的过程,如下所示:

public class Main {

    public static void main() {
        Instant then = Instant.EPOCH;  //1970-01-01T00:00:00Z
        Instant now = Instant.now(); //The current "instant" in time

        Duration duration = Duration.between(then, now);

        //Outputs the days, hours, minutes, and seconds between the epoch start and now.
        System.out.println(duration.getDays());
        System.out.println(duration.getHours());
        System.out.println(duration.getMinutes());
        System.out.println(duration.getSeconds());

        //If you want to account for having already displayed days 
        //when showing your hours, and so on down the units, do this:
        System.out.println(duration.getDays());
        duration = duration.minusDays(duration.getDays());

        System.out.println(duration.getHours());
        duration = duration.minusHours(duration.getHours());

        System.out.println(duration.getMinutes());
        duration = duration.minusMinutes(duration.getMinutes());

        System.out.println(duration.getSeconds());
    }

}

在此处查看完整的软件包文档:http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html