I am trying to get the difference of two dates and get the results in minutes. i have the two dates in milliseconds
long start = 1447143052593L;
long end = 1447146592540L;
output of above is
I/System.out﹕ 03:10:52
I/System.out﹕ 04:09:52
what i expect to be 0:59
And i tried to get the difference in the below way, it does not work.
long mills = end - start;
long Hours = mills/(1000 * 60 * 60);
long Mins = mills % (1000*60*60);
String diff= Hours + ":" + Mins;
And the when i print the String diff i get the result as below
I/System.out﹕ 0:3539947
try this approach:
long Hours = mills / (1000 * 60 * 60);
long Mins = (mills - Hours * (1000 * 60 * 60)) / (1000 * 60);
You are on the correct way, but your Mins now has the milliseconds that are remaining after the hours, instead of the minutes that are remaining.
This should work:
long Mins = (mills % (1000*60*60)) / (1000*60); //make it minutes
Usually you just need to make a Date object
Date startDate = new Date(start);
Date endDate = new Date(end);
int startminutes = startDate.getMinutes();
int endminutes = endDate.getMinutes();
And i say usually because it looks like it has been deprecated some time ago (while i hope it should be still working).
Looks like now they want we to use the Calendar class for this.
Hope this helps.
Related
In my app user pick a date and time from a date and time picker (from the past dates) then I want to start a count up timer from that date and I need to run it in a service so that if my app killed, timer wouldn't stop.
I've tried Chronometer and some similar solution on stackOverFlow but didnt get any correct solution.
Please check below picture, that It is so similar to what I need:
https://ibb.co/rFz24kg
For getting the difference in the dates (here, date2 can be the user-picked date and date1 can be current datetime (which you can get by calling : new Date())):
public static String getDifferenceBetweenDates(Date date1, Date date2) {
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long difference = date2.getTime() - date1.getTime();
long elapsedDays = difference / daysInMilli;
difference = difference % daysInMilli;
long elapsedHours = difference / hoursInMilli;
difference = difference % hoursInMilli;
long elapsedMinutes = difference / minutesInMilli;
difference = difference % minutesInMilli;
long elapsedSeconds = difference / secondsInMilli;
return String.format("%s days %s hours %s minutes %s seconds", elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
}
Now once you get the difference, you can use this as a Count Up Timer and define onTick() to update your view every mCountUpInterval time interval (defined in the CountUpTimer class)
Hi every one i want to devolepe an Alarm App i get the sunrise and sunset time from webservice now i need to manipulate these timing my date store in string when i calculate difference it give correct result when i add two time values it cause problem like
i try it like below to get the manipulated time that i applied manipulation but
it give wrong result
SimpleDateFormat sdf = new SimpleDateFormat("kk:mm:ss");
Date Date1 = sdf.parse(sunrsetat);
Date Date2 = sdf.parse("00:12:00");
long millse = Date1.getTime() + Date2.getTime();
long mills = Math.abs(millse);
int Hours = (int) (mills/(60*60*1000));<------ here it give hour 09 and it must be 19
int Mins = (int) (mills/(1000*60)) % 60;
long Secs = (int) (mills / 1000) % 60;
String time = String.format("%02d:%02d:%02d", Hours, Mins, Secs);
hanfiaiftaritime.setText(time);
Error occurs because Date.getTime() returns millis since Jan 1, 1970, so your mills field has value (assuming date1 < date2) of 2*date1 + difference-in-millis-date2-date1. (http://developer.android.com/reference/java/util/Date.html#getTime())
Solution: use Calendar class.
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
calendar.add(Calendar.SECOND, (int) date2.getTime() / 1000);
long millse = calendar.getTimeInMillis();
In this case computing hours/mins/seconds becomes redundant cause you can use http://developer.android.com/reference/java/util/Calendar.html#get(int) like this:
int hours = calendar.get(Calendar.HOUR_OF_DAY);
Im trying to get the time elapsed from a specific date (in millis)
In some devices I get the correct result
however, in other devices I get wrong result
here's the code that i use:
public static void timeElapsedFromDate(long workingDate)
{
long diff = System.currentTimeMillis() - workingDate;
long diffSeconds = diff / 1000;
long diffMinutes = diffSeconds / 60;
long diffHours = diffMinutes / 60;
long diffDays = diffHours / 24;
long diffWeeks = diffDays / 7;
long diffMonth = diffDays / 30;
}
Isn't it the way to measure time?
thanks!
First of all, diffMonth formula asks to be improved: from Feb 1st to Mar 1st it is usually 28 days, but one month. From Sep 1st to Sep 31st it is 30 days, but less than 1 month.
More subtle question regarding diffDays: isn't it 1 day between 10 PM and 10 AM next morning?
Finally, how different devices can give different results: System.currenTTimeMillis() returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. Even if the clock on all devices is correct, they may have different Time Zone settings.
public int getElapsedTimeInDays(Date start,Date end){
int days=(int)(start.getTime()-end.getTime())/(1000*60*60*24);
return days;
}
public int getElapsedTimeInHours(Date start,Date end){
int hours=(int)(start.getTime()-end.getTime())/(1000*60*60);
return hours;
}
public int getElapsedTimeInMinutes(Date start,Date end){
int minutes=(int)(start.getTime()-end.getTime())/(1000*60);
return minutes;
}
public int getElapsedTimeInSecunds(Date start,Date end){
int scunds=(int)(start.getTime()-end.getTime())/(1000);
return secunds;
}
//Then you can use the codes like this:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd, hh:mm");
Date start = df.parse("2012-01-12, 09:35");
Date end = new Date();//this will be the curent date;
int day = getElapsedTimeInDays(start,end);
Well i'm currently building an app for android and i need to store a day and count how many days until that day comes.
I store the day on shared prefs. First i initialize the calendars.
Calendar next = Calendar.getInstance();
Calendar now = Calendar.getInstance();
Then i set the "next" calendar
nday = prefs.getInt("d", 0);
nmonth = prefs.getInt("m",0);
nyear = prefs.getInt("y",0);
next.set(nyear, nmonth, nday);
Then i do this to calculate how many days left.
diff =next.getTimeInMillis()-now.getTimeInMillis();
diffDays = diff / (24 * 60 * 60 * 1000);
output.setText(diffDays + " Days left");
And here is the problem. The calculator was working great until 2 days ago. When it supposed to say "3 days" it was writing "2 days" and it still goes one day wrong. If i try close and open the app, sometimes it calculates the days correct and sometimes it misses one day... Can someone understands whats wrong? I have diff and diffDays as long. I tried cast them as int but i still got the same problem, sometimes it writes 3 days left, sometimes 2....
ok i found out how to solve this. It seems that the getInstance have difference in milliseconds so i did this
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY,00);
now.set(Calendar.MINUTE ,00);
now.set(Calendar.SECOND,00);
now.set(Calendar.MILLISECOND,00);
get the day from shared prefs
nday = extras.getInt("nDay");
nmonth = extras.getInt("nMonth");
nyear = extras.getInt("nYear");
//set the calendar
next.set(nyear, nmonth, nday, 00, 00,00);
next.set(Calendar.MILLISECOND,00);
and finally calculate the difference
long diff = 0;
diff = next.getTimeInMillis()-now.getTimeInMillis();
diffDays = diff / (24 * 60 * 60 * 1000);
output.setText(diffDays + " Days");
now i get the real difference without any mistakes, thanks everyone for your help!
Because it is implicitly casting to long, so you are losing some of your minutes and hours.
Declare diffDays as double, then you can show to the user when there are, for example 2.5 days and it won't show it as 2 days. Or, you could take the integer part for the days, and calculate the hour from the fraction:
int hour = (diffDays - Math.floor(diffDays))*24
This gives you the minute. Just make sure you did declare diffDays as double or float.
Also use the calendar for calculating dates.
Calendar diff = Calendar.getInstance();
long diffMillis = next.getTimeInMillis()-now.getTimeInMillis();
diff.setTimeInMilliSeconds(Math.abs(diffMillis));
int days = diff.get(Calendar.DAY_OF_YEAR);
if (diffMillis < 0L) {
days *= -1;
}
i want to run an countdown time , in which i want to show days,hours,sec and milisec remaining for a specific date. and will be be keep changing till the end of the specific date.
Hope you can understand.
Thanks in advance.
Well, I think the problem is, that you dont know, how to work with the time. Here i have a method I use to calculate the amount of time of some items which I parse out of a db.
The param is a double value, which has got the whole time in seconds. It returns a string with the time in days, hours, minutes and seconds as string.
public String timeCalculate(double ttime) {
long days, hours, minutes, seconds;
String daysT = "", restT = "";
days = (Math.round(ttime) / 86400);
hours = (Math.round(ttime) / 3600) - (days * 24);
minutes = (Math.round(ttime) / 60) - (days * 1440) - (hours * 60);
seconds = Math.round(ttime) % 60;
if(days==1) daysT = String.format("%d day ", days);
if(days>1) daysT = String.format("%d days ", days);
restT = String.format("%02d:%02d:%02d", hours, minutes, seconds);
return daysT + restT;
}
For the countdown itself...take the target timestamp minus the actual one and voila, you've got seconds left :) Put those seconds to this method and you've got the remaining time. Now you just need to do some UI things ;)
Oh, and for the usual Unix Timestamp you can use this little method:
public static long getTimestamp() {
return System.currentTimeMillis() / 1000;
}