-2

I am getting day and time in this format dd.MM. HH:mm from server. I want to get current city day and time irrespective of where I am staying in the same format and check if its less than than 20 mins. How do I do this?

MagdHo0506
  • 73
  • 8

1 Answers1

0

I have used this code to get difference in min. It require date with year also.

public static int getDifferenceTimeInMin(String startDate, String endDate)
{

        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat(getString(R.string.common_dateTime_format));


        Date date2 = null;
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(startDate);
            date2 = simpleDateFormat.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        //milliseconds
        long different = date1.getTime() - date2.getTime();

        //System.out.println("startDate : " + startDate);
        //System.out.println("endDate : "+ endDate);
        System.out.println("different : " + different);

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        System.out.printf(
                "%d days, %d hours, %d minutes, %d seconds%n",
                elapsedDays,
                elapsedHours, elapsedMinutes, elapsedSeconds);

       // int Hours = Integer.parseInt(String.valueOf(elapsedHours));

        return  Integer.parseInt(String.valueOf(elapsedMinutes));

}

Hope it will help...

Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55