简体   繁体   中英

Difference between current stop and start time in android studio java language

I have two button start and stop. it get current time when i click on these button and show me the current time in textview. i want to make diffienece between these textview time and want to add the start, stop and difference of time to save in sql database. my code is here.

public class Aircraft extends Fragment {

    TextView tvstarttime;
    Button btn_start;
    Button btn_end;
    TextView tvend;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_aircraft, container, false);

        tvstarttime = view.findViewById(R.id.txtstarttime);
        btn_start = view.findViewById(R.id.btn_start);
        btn_end= view.findViewById(R.id.btn_end);
        tvend = view.findViewById(R.id.txtendtime);
       btn_start.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {

               Calendar calendar = Calendar.getInstance();
               SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
               String time = format.format(calendar.getTime());


               tvstarttime.setText(time);
           }
       });
        btn_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
                String time = format.format(calendar.getTime());


                tvend.setText(time);
            }
        });


        return view;
    }






}

You can find difference between two timestamp and store it to your db like,

 long startTime = 0L;
 long endTime = 0L;
    
    btn_start.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                // Do your code here
           
           Calendar calendar = Calendar.getInstance();
           SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
           String time = format.format(calendar.getTime());


           tvstarttime.setText(time);
    
                startTime = System.currentTimeMillis()
    
               }
    });

    btn_end.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
               // Do your code here
   
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
            String time = format.format(calendar.getTime());


            tvend.setText(time);

                endTime = System.currentTimeMillis()
                }
    });
    
    //calculate Time difference, but make sure you have put proper validations for end time and start time so difference can not be negative

    difference = endTime - startTime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM