0

How to calculate the difference in time in minutes? I used 2 templates T1 and T2 to select the time and tried to use function but I couldn't do it

>**first clock**
        *T1*.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimePickerDialog timePickerDialog = new TimePickerDialog(  
                        MainActivity.this,
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                t1h = hourOfDay;
                                t1m = minute;
                                Calendar calendar = Calendar.getInstance();
                                calendar.set(0, 0, 0,t1h,t1m);
                        T1.setText(android.text.format.DateFormat.format( "hh-mm a", calendar));
                   }}, 12, 0, false);
                timePickerDialog.updateTime(t1h,t1m);
                timePickerDialog.show();
            } });
// **second clock**
        T2.setOnClickListener(new View.OnClickListener() {
       // similiar cod
// **try to canculate but i can't**
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("hh-mm");
                double c,x,y;
                String S3 = T1.getText().toString();
                String S4 = T2.getText().toString();
                x = Double.parseDouble(S3);
                y = Double.parseDouble(S4);
                c = y - x;
                String S = Double.toString(c);
                sr.setText(S);
            }    });  }}

https://github.com/Icefenix1996/time-calculate

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
slava
  • 11
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. See the answer by Arvind Kumar Avinash. – Ole V.V. Aug 19 '21 at 06:08

1 Answers1

2

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API: Use java.time.Duration to calculate the duration(difference) between two times. Duration is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation. With Java-9 some more convenience methods were introduced.

Demo:

import java.time.Duration;
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        LocalTime start = LocalTime.of(10, 20, 30);
        LocalTime end = LocalTime.of(11, 25, 40);
        Duration duration = Duration.between(start, end);
        System.out.println(duration);

        // Custom format
        // ####################################Java-8####################################
        String formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHours() % 24,
                duration.toMinutes() % 60, duration.toSeconds() % 60);
        System.out.println(formattedElapsedTime);
        // ##############################################################################

        // ####################################Java-9####################################
        formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHoursPart(), duration.toMinutesPart(),
                duration.toSecondsPart());
        System.out.println(formattedElapsedTime);
        // ##############################################################################
    }
}

Output:

PT1H5M10S
01:05:10
01:05:10

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110