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?
Asked
Active
Viewed 135 times
-2

MagdHo0506
- 73
- 8
-
I need it ASAP :( – MagdHo0506 Mar 06 '17 at 17:43
-
Possible duplicate of [Android - Compare two dates](http://stackoverflow.com/questions/33032762/android-compare-two-dates) – Pike D. Mar 06 '17 at 17:45
-
What have you tried? Post some relevant code and point out where you are having trouble? – Andrew Mar 07 '17 at 01:31
-
1Need it asap? I suppose someone forgot to hand you the assignment until just before it was due right :). – Andrew Mar 07 '17 at 01:32
1 Answers
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