Calculate Difference between two times in Android
Try below code.
// suppose time format is into ("hh:mm a") format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a");
date1 = simpleDateFormat.parse("08:00 AM");
date2 = simpleDateFormat.parse("04:00 PM");
long difference = date2.getTime() - date1.getTime();
days = (int) (difference / (1000*60*60*24));
hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60));
min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
hours = (hours < 0 ? -hours : hours);
Log.i("======= Hours"," :: "+hours);
Output - Hours :: 8
Time difference between two times
To Calculate the difference between two dates you could try something like:
long millis = date1.getTime() - date2.getTime();
int hours = (int) (millis / (1000 * 60 * 60));
int mins = (int) ((millis / (1000 * 60)) % 60);
String diff = hours + ":" + mins;
To update the Time Difference every second you can make use of Timer.
Timer updateTimer = new Timer();
updateTimer.schedule(new TimerTask() {
public void run() {
try {
long mills = date1.getTime() - date2.getTime();
int hours = millis/(1000 * 60 * 60);
int mins = (mills/(1000*60)) % 60;
String diff = hours + ":" + mins; // updated value every1 second
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 1000); // here 1000 means 1000 mills i.e. 1 second
Edit : Working Code :
public class MainActivity extends Activity {
private TextView txtCurrentTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCurrentTime= (TextView)findViewById(R.id.mytext);
Timer updateTimer = new Timer();
updateTimer.schedule(new TimerTask()
{
public void run()
{
try
{
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
Date date1 = format.parse("08:00:12 pm");
Date date2 = format.parse("05:30:12 pm");
long mills = date1.getTime() - date2.getTime();
Log.v("Data1", ""+date1.getTime());
Log.v("Data2", ""+date2.getTime());
int hours = (int) (mills/(1000 * 60 * 60));
int mins = (int) (mills/(1000*60)) % 60;
String diff = hours + ":" + mins; // updated value every1 second
txtCurrentTime.setText(diff);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}, 0, 1000);
}
Difference between two times in Android Studio, (Registered time and current time)
This answer lead me the right way, actually my approach was good I just needed to make a function that showed only the difference.
I made this one. Hope it helps somebody.
Just call the function with the result of you operation, at the end I used Date().getTime() instead of System.currentTimeMillis() but I think it should be the same.
The complete code:
startTime = System.currentTimeMillis().toDouble()
timer = Timer()
timer.schedule(timerTask {
runOnUiThread {
advanceTimer()
}
}, 0, 60)
fun advanceTimer() {
//Total time since timer started, in seconds
val currentTime = System.currentTimeMillis().toDouble()
time = currentTime-startTime
// Shows the time in a label on the screen
timerString.text = differenceResult(time)
}
fun differenceResult(time: Long): String {
var x: Long = time / 1000
var seconds = x % 60
x /= 60
var minutes = x % 60
x /= 60
var hours = (x % 24).toInt()
x /= 24
var days = x
return String.format("%02d:%02d:%02d", hours, minutes, seconds)
}
How to calculate difference between two times in Java?
You could take two dates/times and minus them from each other to get a duration.
This duration can then be converted into whatever format you need.
The example is an example, your mileage may vary.
LocalDateTime fromDateTime = LocalDateTime.of(2019, 10, 07, 7, 45, 55);
LocalDateTime current = LocalDateTime.now();
long duration = current.toEpochSecond(ZoneOffset.UTC) - fromDateTime.toEpochSecond(ZoneOffset.UTC);
double minutes = duration/60.0;
double hours = duration/3600.0;
System.out.println("Minutes: "+ minutes);
System.out.println("Hours: "+ hours);
Related Topics
Java.Lang.Illegalmonitorstateexception: Object Not Locked by Thread Before Wait()
Android Import Java.Nio.File.Files; Cannot Be Resolved
Creating Hashmap from a JSON String
What Is the Easiest Way to Get the Current Day of the Week in Android
Opengl Extensions Available on Different Android Devices
Android Resource - Array of Arrays
Intellij Idea with Junit 4.7 "!!! Junit Version 3.8 or Later Expected:"
Elegant Way to Read File into Byte[] Array in Java
How to Use Okhttp to Upload a File
How to Know Whether I am in a Call on Android
What's a Good Library for Parsing Mathematical Expressions in Java
Recyclerview - Get View at Particular Position
How to Read Jpeg Image Using Imageio.Read(File File)
Android Simpledateformat, How to Use It
Android Heterogeneous Gridview Like Pinterest
Android: Prompt User to Save Changes When Back Button Is Pressed