I am getting UTC date from db. And subtracting that from current utc DateTime. I would like to have diff. in minutes. Something in not quite right.
Date currenttimedate = DateTime.UTCNow();
String lastsynctime = c.getString(c.getColumnIndex(columnname)).toString();
Date lastruntimedate = DateTime.StringDateToDate(lastsynctime, "MM/dd/yyyy HH:mm:ss");
long diffInMs = (currenttimedate. getTime() - lastruntimedate.getTime());
// convert it to Minutes
long diffInMins = TimeUnit.MILLISECONDS.toMinutes(diffInMs);
// return time in minutes
response = (int)(diffInMins);
It's returning something like 720 minutes. Expected answer can not be more than 5. As data is synced every 5 mins.
Related
I am saving current date in timestamp value in my firebase with below code
userValues.put("p_date", ServerValue.TIMESTAMP);
what I need to do is show a time difference between above saved value and current timestamp value, I have tried below code
//time ago
String starttime = book.getP_date().toString(); //getting saved timestamp from firebase
//current time
Long tsLong = System.currentTimeMillis()/1000;
String endtime = tsLong.toString();
long diffTime = Long.parseLong(endtime) - Long.parseLong(starttime);
String elapsedtime = new SimpleDateFormat("hh:mm:ss").format(Long.parseLong(String.valueOf(diffTime)));
tvTimeAgo.setText(elapsedtime+"ago");
it is giving a result like 07:59:56 ago shown time difference is returning wrong result also what I need to have is 0h 5min 3s ago
how can I achieve this?
You can use TimeUnit.MILLISECONDS for formate TimeStamp as below
String timeHH_MM_SS = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(diffTime),
TimeUnit.MILLISECONDS.toMinutes(diffTime) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(diffTime) % TimeUnit.MINUTES.toSeconds(1));
tvTimeAgo.setText("Time in hh:mm:ss is: "+timeHH_MM_SS);
I have a method which takes two String parameters. the two strings are Time values in 24 hour format. The Times are picked using a TimePicker from UI.
The goal of the method is to get the duration between the StartTime and EndTime in Minutes.
public static String getTimeDuration(String StartTime24, String EndTime24)
{
String duration = "";
try
{
SimpleDateFormat parseFormat = new SimpleDateFormat("HH:mm");
Date startTime = parseFormat.parse(StartTime24);
Date endTime = parseFormat.parse(EndTime24);
long mills = endTime.getTime() - startTime.getTime();
long minutes = mills/(1000 * 60);
duration = "(" + minutes + " Minutes)";
}
catch(ParseException ex)
{
// exception handling here
}
return duration;
}
The method works fine if both the times are within a Single Date. For example:
StartTime = 22:15
EndTime = 23:51
Output = (96 Minutes)
But my problem is, the method returns negative if the end time is after 12'o clock at night. For example,
StartTime = 23:51
EndTime = 0:55
Output = (-1376 Minutes)
What I want: (64 Minutes)
How can get the duration correct ?
As there is no date used, you have to check first if your endTime is less than your startTime. If yes, then your endTime is on the next day and you have to add 1 day/86 400 000 milliseconds. Then you will have your desired result.
Just add this condition:
if(endTime.getTime() < startTime.getTime()){
long mills = ((endTime.getTime() + 86400000) - startTime.getTime()); 1 day = 86 400 000 mill
}
Hope this helps
You're only parsing the minutes and hours. There's no day on there. So that puts both times on the same day (the first day of the epoch, Jan 1 1970 to be exact). So the answer is correct. If you want it to treat the end time as the next day if its earlier than the start time, then add 1 day to the result (1440 minutes) if the result is less than 0.
I want my app to show the time elapsed from the point an entry was stored in the database to the point it is fetched from the database.
I am currently doing something like this which gives me time elapsed in seconds:
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss aa");
Date systemDate = Calendar.getInstance().getTime();
String myDate = sdf.format(systemDate);
Date Date1 = sdf.parse(myDate);
Date Date2 = sdf.parse(savedDate);
int dateDiff = getTimeRemaining();
long millse = Date1.getTime() - Date2.getTime();
long mills = Math.abs(millse);
long Mins = mills/(1000*60);
String diff = +dateDiff+ "days " + Mins+ " mins";
cal[1] = "" +Mins;
t3.setText(diff);
But I also want it to include the no of days since the data was stored. As of now, it resets when the day is over. I want it to give me the total minutes after N days. How should I do it? Thanks in advance.
You firstly need to determine the number of seconds from database-stored-time until now.
long ageOfDatabaseEntry = (System.currentTimeMillis() - databaseEnteredTimeMillis)
You then need to determine how many days you want, then modulo the age by that number to get the remaining number of milliseconds.
long getRemainingMinutes(long age, int days) {
// Use the modulus operator to get the remainder of the age and days
long remainder = age % (days * 86400000);
if(remainder == age) {
throw new InvalidArgumentException("The number of days required exceeds the age of the database entry. Handle this properly.");
}
// Turn the remainder in milliseconds into minutes and return it
return remainder / 60000;
}
I want to convert TimeUnit to seconds or hours , I've asked a lot but do not get a satisfactory answer.
I've read on http://developer.android.com/reference/java/util/concurrent/TimeUnit.html#toHours(long) but I don't understand to use
example:
String strfileDate = "2012-04-19 15:15:00";
DateFormat formatter2 = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date da =formatter2.parse(strFileDate);
long diffInMs = da.getTime() - new Date(System.currentTimeMillis()).getTime();
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);
I want to convert valuse of "diffInSec" to Seconds, hours
convert day to hours, should be TimeUnit.DAYS.toHours(days).
for other type, change Enum value for source time unit.
Essentially what I have is a string which contains a files Last Modified Date. To get this I'm using:
Date lastModDate = new Date(file.lastModified());
SimpleDateFormat formatter = new SimpleDateFormat("K:mm a");
String formattedDateString = formatter.format(lastModDate);
The end result is somewhat like 6:12 AM. What I want to do is each time a certain period of time is passed, the dateformat must change. E.g.
After 1 Day has gone by, Last Modified Date = ("Format1");
After a Week has gone by, Last Modified Date = ("Format2");
After 2 Weeks have gone by, Last Modified Date = ("Format3");
Does it make sense? If so is someone please be able to show me how it's done. A good example is the native Messaging App. When a message is created, It will show it's Time then after some days gone by the format changes to the Date it was created then the month etc...
I'm trying to do exactly that.
Calculate the difference in time between the last modified date and now:
long duration = lastModDate.getTimeInMillis() - current.getTimeInMillis();
long sec = TimeUnit.MILLISECONDS.toSeconds(duration);
boolean inFuture = sec > 0;
// Use positive value
if(!inFuture)
sec = -sec;
long minutes = sec / 60 % 60;
long hours = sec / 3600 % 24;
long days = sec / 86400;
if(days > 1 && days < 7)
// Use format 1
else if(days >= 7 && days < 14)
// Use format 2
else
// Use format 3