-1

I need help. I have two dates.

private Date endDate;
private Date now;

public String getRemainingTime(endDate) {
   now = new Date();

   //logic

}

And also I have method whic should return difference between two dates in String formate, if difference more than 1 day - "1 day 15 min" for example;

if difference more than 1 hour but less than 1 day - "1 hour 13 min" for example;

and if difference less than 1 hour - "35 min 39sec", something like this...

Please help me, I'm not familiar with java.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
user3673623
  • 1,795
  • 2
  • 12
  • 18
  • 2
    possible duplicate of [How to compare dates in Java?](http://stackoverflow.com/questions/2592501/how-to-compare-dates-in-java) – weston Nov 11 '14 at 09:06
  • 3
    Another question like this.. [See Here](http://stackoverflow.com/questions/17940200/how-to-find-the-duration-of-difference-between-two-dates-in-java) – bud-e Nov 11 '14 at 09:08
  • One possible solution might be getting more familiar with Java. This helps when programming with it... – Uwe Allner Nov 11 '14 at 09:15
  • You may want to learn about the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) standard which defines unambiguous string representation of a duration: `P[n]Y[n]M[n]DT[n]H[n]M[n]S`. Both Joda-Time and java.time libraries can parse and generate these strings. – Basil Bourque Nov 11 '14 at 17:24

4 Answers4

1

if suggested comments link doesn't workout, then try this: `

Date endDate;
         Date now;
         StringBuilder sb = new StringBuilder();
        //timestamp difference in millis 
        Long diff = endaDate.getTime() - now.getTime();
        //get the seconds
        long seconds = diff / 1000;
        //get the minutes
        long minutes = diff / (1000 * 60);
        //get the hours
        long hrs = diff / (1000 * 60 * 60);
        if (hrs > 0) {
            sb.append(hrs + " hrs");
            if (minutes % 60 > 0) {
                sb.append(", " + minutes % 60 + " mins");
            }
            if (seconds % 60 > 0) {
                sb.append(", " + seconds % 60 + " secs");
            }
        } else if (minutes % 60 > 0) {
            sb.append(minutes % 60 + " mins");
            if (seconds % 60 > 0) {
                sb.append(", " + seconds % 60 + " secs");
            }
        } else if (seconds % 60 > 0) {
            sb.append(seconds % 60 + " secs");
        } else {
            sb.append("00");
        }
        System.out.println(sb.toString());

`

Rahul Sharma
  • 5,614
  • 10
  • 57
  • 91
1

Updated
1) This code will do the job (compiler will wrap constants):

public String getIntervalAsString(Date date1, Date date2) {
        String format;

        long dT = date2.getTime() - date1.getTime();
        if (dT < 1000 * 60)
            format = "s 'sec'";
        else if (dT < 1000 * 60 * 60)
            format = "m 'min' s 'sec'";
        else if (dT  < 1000 * 60 * 60 * 24)
            format = "h 'hour(s)' m 'min'";
        else if (dT < 1000 * 60 * 60 * 24 * 365)
            format = "d 'day(s)' h 'hour(s)'";
        else
            format = "'more than a year'";

        SimpleDateFormat formatter = new SimpleDateFormat(format);

        return formatter.format(new Date(dT));
    }

2) you may try different patterns here

sberezin
  • 3,266
  • 23
  • 28
  • Rolling your own code is a bad idea when we have excellent well-worn libraries available, such as Joda-Time and java.time. This is especially true for date-time work which is tricky and error-prone. – Basil Bourque Nov 11 '14 at 17:21
  • Main work is done by SimpleDateFormat which belongs to java.text. IMHO it's superfluous (as for time spent so for size of apk) to attach non-standard library when it takes couple of lines. – sberezin Nov 11 '14 at 21:12
1
private static final long MILLIS_PER_SECOND = 1000L;
private static final long MILLIS_PER_MINUTE = MILLIS_PER_SECOND * 60L;
private static final long MILLIS_PER_HOUR = MILLIS_PER_MINUTE * 60L;
private static final long MILLIS_PER_DAY = MILLIS_PER_HOUR * 24L;

public String getRemainingTime(Date endDate) {
    long remain = endDate.getTime() - System.currentTimeMillis();
    if (remain <= 0L) {
        return "pass";
    }
    long days = remain / MILLIS_PER_DAY;
    long hours = (remain % MILLIS_PER_DAY) / MILLIS_PER_HOUR;
    long minutes = (remain % MILLIS_PER_HOUR) / MILLIS_PER_MINUTE;
    long seconds = (remain % MILLIS_PER_MINUTE) / MILLIS_PER_SECOND;
    StringBuilder sb = new StringBuilder();
    if (days > 0L) {
        sb.append(days).append(" day ");
    }
    if (hours > 0L || days > 0L) {
        sb.append(hours).append(" hour ");
    }
    if (minutes > 0L || days > 0L || hours > 0L) {
        sb.append(minutes).append(" min ");
    }
    if (seconds > 0L) {
        sb.append(seconds).append(" sec");
    }
    return sb.toString();
}
Jarod
  • 1,662
  • 1
  • 11
  • 19
0

The Joda-Time library might match your needs:

DateTime now
DateTime endDate;
Period p = new Period(now, endDate);
long hours = p.getHours();
long minutes = p.getMinutes();
user1438038
  • 5,821
  • 6
  • 60
  • 94