I want to write a program which shows the time remained from right now to a specific time in a date in future. as I want to use Joda-Time
I searched alot and found some codes(e.g. this one) but I dont know how to do that for 2 different times in 2 different days. any idea?
Asked
Active
Viewed 164 times
0
-
Slightly confused as to what you need. Do you just want a diff between "now" and a specific date in the future? If so, thats easy to do using Period. Confused by " 2 different times in 2 different days" – Jimmy Mar 12 '15 at 22:56
-
1Can you post whatever code you have so far? It may help us understand what you're trying to do. – Jordan Mar 12 '15 at 22:59
-
What are your inputs and expected outputs? – MadProgrammer Mar 12 '15 at 23:11
-
@MadProgrammer I want to give 2 Dates(year,month,day,hour,minute,second) and it gives me the duration between them(day,hour,minute,second) – Soheil Mar 12 '15 at 23:15
3 Answers
4
You mean something like...
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");
Which outputs
2 years
10 months
10 days
8 hours
20 seconds
950 millis
Now using...
DateTime from = new DateTime();
DateTime to = new DateTime(2015, 3, 21, 2, 15, 10);
instead, prints...
0 years
0 months
7 days
15 hours
26 seconds
755 millis

MadProgrammer
- 343,457
- 22
- 230
- 366
-
-
@MadProgrammer thanx but I'm getting strange result! `LocalDateTime from = new LocalDateTime(); LocalDateTime to = new LocalDateTime(2015, 3,21, 2, 15, 10);` gives me 0days. why???!! – Soheil Mar 12 '15 at 23:29
-
Yes I noted this too after running this program. Even for your input it should not give 3 days I believe. @MadProgrammer – muasif80 Mar 12 '15 at 23:33
-
1@muasif80 See update - Shows you how often I actually do this :P – MadProgrammer Mar 12 '15 at 23:40
-
:) So what are the updates? The way you are getting period object now is the update or the change to DateTime. I am going to run this now too though. – muasif80 Mar 12 '15 at 23:42
-
-
@muasif80 No, it needs a `DateTime` object, which carries with it time zone information. It might be possible to figure it out, but this got it working – MadProgrammer Mar 12 '15 at 23:48
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());
}
}
I think this might be helpful for you.
This code will give exact same durations. You need to set the calendar2 to a fixed date in future to get your desired result.
Update 1
You can have it like this for example for a fixed date in future. There can be other better ways to achieve same.
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");
}
}

muasif80
- 5,586
- 4
- 32
- 45
-
That's an interesting way to use `Duration`. Any reason why you wouldn't just use `LocalDateTime` instead? – MadProgrammer Mar 12 '15 at 23:10
-
I don't know about LocalDateTime :) Its jodatime specific i think. Not have used JodaTime lot. Will be happy to learn from you. – muasif80 Mar 12 '15 at 23:13
-
You mean like [`org.joda.time.LocalDateTime`](http://www.joda.org/joda-time/apidocs/org/joda/time/LocalDateTime.html)? – MadProgrammer Mar 12 '15 at 23:15
-
-
@Soheil How doesn't it work correctly? What is your input and expected output? – MadProgrammer Mar 12 '15 at 23:26
-
-
@Soheil I had edited it and now again made little change. It might have worked for you now. But that org.joda.time.Period should be the right way to achieve this. Don't know why it is not giving correct days. – muasif80 Mar 12 '15 at 23:40
-
1
You mentioned that you want to use Joda-time, so one of the other solutions may be better for you.
However, this same process can now also be done quite easily using Java 8's native Time library as follows:
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());
}
}
Check out the full package documentation here: http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

bertag
- 417
- 1
- 3
- 10
-
Awesome, but the question does state that they want/need to use Joda-Time and given the fact that they want days, hours, minutes and seconds between the two dates, this would suggest that `Period` would be a better choice – MadProgrammer Mar 12 '15 at 23:28
-
@MadProgrammer, I'm with you on this one. I actually debated whether I should answer, given the Joda-time request and ultimately figured it couldn't hurt and could be useful to the discussion. Per your comment, I have updated the code to show the output in days, hours, minutes, and seconds. – bertag Mar 12 '15 at 23:33