How to make a count up timer from a specific date? - android

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)

Related

How to calclutate different between two date in android

I have android application and I need to display the different between two date but I have a problem in this case
For example :
When tried to display the different between date 28/12/2016 and 01/01/2017
the result is 339 days , How I can solve this and when tried to display, I got a correct result. This is my code:
public void Cal() {
java.text.SimpleDateFormat simpleDateFormat =new java.text.SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
try {
Date date2 = simpleDateFormat.parse("2017/01/01"+ " " + "11:30:00");//end_date
Date date1 = simpleDateFormat.parse("2016/12/28"+ " " + "11:30:00");//start_date
printDifference(date1, date2);
} catch (ParseException e) {
e.printStackTrace();
}
}
public void printDifference(Date startDate, Date endDate) {
long different = endDate.getTime() - startDate.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);
}
The pattern you used when constructing your SimpleDateFormat has a problem. You used this:
yyyy/mm/dd hh:mm:ss
But you should have used this:
yyyy/MM/dd hh:mm:ss
From the documentation, lowercase m means minutes in an hour, which is appropriate for the timestamp portion of your format string, while uppercase M means months in the year.

sqlite query to get date difference in android working on netbeans

I am making a reminder app and for that I need to calculate the date difference between the stored date in database with the current date. I have tried the julianday method but it doesn't seems to work. Please give the relevant sqlite query. I am new to android so please help!!!
At First Query Data from database for getting date then convert this date data to Date Format
Covert Date to Date Format
String dateString = "03/26/2012 11:49:00 AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Then get System Current date and Compare database data to System current date.
Date Different Method
public void printDifference(Date startDate, Date endDate){
//milliseconds
long different = endDate.getTime() - startDate.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);
}

Android get the date difference in minutes

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.

Subtract two datepickers in android [duplicate]

This question already has answers here:
How do I calculate someone's age in Java?
(28 answers)
Closed 8 years ago.
Is there any way to subtract two dates from picker.
Example:
I will select a date from picker1 and date from picker2.
Then, picker2 - picker1 to get the number of days between these two dates.
20/10/2014
(-) 06/10/2014
--------------
14 days
DateTimeUtils obj = new DateTimeUtils();
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd/M/yyyy hh:mm:ss");
try {
Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
Date date2 = simpleDateFormat.parse("13/10/2013 20:35:55");
obj.printDifference(date1, date2);
} catch (ParseException e) {
e.printStackTrace();
}
}
//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void printDifference(Date startDate, Date endDate){
//milliseconds
long different = endDate.getTime() - startDate.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);
}
output is this
startDate : Thu Oct 10 11:30:10 SGT 2013
endDate : Sun Oct 13 20:35:55 SGT 2013
different : 291945000
3 days, 9 hours, 5 minutes, 45 seconds
Answer from this link Android difference between Two Dates
Since not much information is provided, I assume you are using DatePicker
The basic flow is like that:
by calling getDayOfMonth(), getMonth() and getYear() from DataPicker to obtain day, month and year respectively.
after obtaining those information, I think you should be able to calculate the day difference between them.
Just for your reference, the following is a topic about day difference, hope this help.
Difference in days between two dates in Java?
short and simple
SimpleDateFormat simpleDateFormat= new SimpleDateFormat("dd/MM/yyyy");
java.util.Date date1 = null;
java.util.Date date2 = null;
try {
date1 = simpleDateFormat.parse("20/10/2014");
date2 = simpleDateFormat.parse("06/10/2014");
} catch (java.text.ParseException e) {
e.printStackTrace();
}
int diffInDays = (int) ((date1.getTime() - date2.getTime())/ (1000 * 60 * 60 * 24));
EDIT :
Hello Uday
it is Very Simple you have two EditText so first Set your date into et and et1 and now
et.getText().toString(); //Do same for et1 also
this mothod will helps you for getting date and now apply to my code

Calculating elapsed time between two dates, gives different results in different devices

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);

Categories

Resources