我想写一个程序,显示从现在到将来某个特定时间的剩余时间。因为我想使用Joda-Time
我搜索了很多并找到了一些代码(例如this one)但我不知道如何在2个不同的日子里做2次不同的时间。任何想法?
答案 0 :(得分:4)
你的意思是......
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
答案 1 :(得分:3)
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");
}
}
答案 2 :(得分:1)
你提到你想要使用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