-4

Want to find difference between two times I have two times 12:16:40 and 12:16:50. I want difference is 10 seconds if the difference is in minutes then display the difference in minutes or if the difference is in seconds then display the difference seconds and so on.

I want an answer like this

2 sec ago

5 mins ago

2 hours ago

Date date = new Date();
DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); // Hours:Minutes:Seconds
String dateFormatted = formatter.format(date); 
BhalchandraSW
  • 714
  • 5
  • 13
Ahmad
  • 47
  • 1
  • 11
  • So you want to display a time difference expressed in milliseconds differently dependent on the size of the difference? Use some if statements to distinguish the cases. – Henry Jun 16 '17 at 07:41
  • 1
    Possible duplicate of [Time difference between two times](https://stackoverflow.com/questions/15360123/time-difference-between-two-times) – Basil Bourque Jun 16 '17 at 08:01
  • Search Stack Overflow before posting. This topic has been addressed *many* times already. – Basil Bourque Jun 16 '17 at 08:02

4 Answers4

0

Hi you can use below code this is the example i have given you need to make changes according to your requiremnts

try {

                Calendar c2 = Calendar.getInstance();
                SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss aa",Locale.US);
                String formattedTime = df2.format(c2.getTime());
                date2 = df2.parse(formattedTime);

                long mills = date2.getTime() - date1.getTime();


                int Hours = (int) (mills/(1000 * 60 * 60));
                int Minuets = (int) (mills/(1000*60)) % 60;
                int Seconds = (int) (mills/1000) % 60;

                String hr = String.format("%02d", Hours);
                String min = String.format("%02d", Minuets);
                String sec = String.format("%02d", Seconds);


                endTime = hr + ":" + min + ":" +sec; // updated value every1 second

            } catch (ParseException e) {
                e.printStackTrace();
            }

So for your question use below function to get answer and import below clases

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

private void cluclate()
{
    Date date1,date2;

    try {

        String  endTime;

        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.HOUR,12);
        c1.add(Calendar.MINUTE,16);
        c1.add(Calendar.SECOND,40);
        SimpleDateFormat df1 = new SimpleDateFormat("HH:mm:ss", Locale.US);
        String formattedTime1 = df1.format(c1.getTime());
        date1 = df1.parse(formattedTime1);


        Calendar c2 = Calendar.getInstance();
        c2.add(Calendar.HOUR,12);
        c2.add(Calendar.MINUTE,16);
        c2.add(Calendar.SECOND,50);
        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss",Locale.US);
        String formattedTime2 = df2.format(c2.getTime());
        date2 = df2.parse(formattedTime2);


        long mills = date2.getTime() - date1.getTime();


        int Hours = (int) (mills/(1000 * 60 * 60));
        int Minuets = (int) (mills/(1000*60)) % 60;
        int Seconds = (int) (mills/1000) % 60;

        String hr = String.format("%02d", Hours);
        String min = String.format("%02d", Minuets);
        String sec = String.format("%02d", Seconds);


        endTime = hr + ":" + min + ":" +sec; // updated value every1 second

        Log.d("=============>>>>",endTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }
}
Anil
  • 1,605
  • 1
  • 14
  • 24
0

Try this:

long firstTime = System.currentTimeMillis();
    /*
    your operations should do here
     */
    long secondTime = System.currentTimeMillis();
    long difference = secondTime - firstTime;
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(difference);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss", Locale.GERMANY);
    String yourDate = simpleDateFormat.format(calendar);
grabarz121
  • 616
  • 5
  • 13
0

Use the following code in a runnable, so that it keeps on checking.

Calendar c2 = Calendar.getInstance();
SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss aa",Locale.US);
String formattedTime = df2.format(c2.getTime());
date2 = df2.parse(formattedTime);
//date1 is the first time, date2 is current time.
long mills = date2.getTime() - date1.getTime();

int Hours = (int) (mills/(1000 * 60 * 60));
int Minuets = (int) (mills/(1000*60)) % 60;
int Seconds = (int) (mills/1000) % 60;

if(Hours == 0) {
    if(Minutes == 0) {
        displayString = Seconds.toString() + " ago";
    } else {
        displayString = Minutes.toString() + " ago";
    }
} else {
    displayString = Hours.toString() + " ago";
}
dustblue
  • 557
  • 5
  • 14
0

This can be easily achieved using DateUtils class.

getRelativeTimeSpanString(long time, long now, long minResolution)

Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, 12);
time.set(Calendar.MINUTE, 16);
time.set(Calendar.SECOND, 40);

Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR, 12);
now.set(Calendar.MINUTE, 16);
now.set(Calendar.SECOND, 50);
System.out.println(DateUtils.getRelativeTimeSpanString(time.getTimeInMillis(), now.getTimeInMillis(), DateUtils.SECOND_IN_MILLIS));

This will also be valid if the time difference is in minutes or hours.

BhalchandraSW
  • 714
  • 5
  • 13
  • If i will set time time.set(Calendar.HOUR, 12); time.set(Calendar.MINUTE, 16); time.set(Calendar.SECOND, 40); Calendar now = Calendar.getInstance(); now.set(Calendar.HOUR, 2); now.set(Calendar.MINUTE, 11); now.set(Calendar.SECOND, 50); it will return in 10 hours – Ahmad Jun 16 '17 at 09:13
  • It depends on the date in time and now variables. – BhalchandraSW Jun 16 '17 at 09:23