0

I have this code

package com.lestijden;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView Datum;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Datum = (TextView) findViewById(R.id.Tijd);

    Date today = Calendar.getInstance().getTime();
    SimpleDateFormat Formatter = new SimpleDateFormat("km");
    String folderName = Formatter.format(today);

    int result = folderName.compareTo("0");


    if (result > 815 ){
        double data = new Double("905") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }
    else if (result > 905){
        double data = new Double("955") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }

    else if (result > 955){
        double data = new Double("1045") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }

    else if (result > 1045){
        double data = new Double("1100") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }

    else if (result > 1100){
        double data = new Double("1150") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }
    else if (result > 1150){
        double data = new Double("1240") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }
    else if (result > 1240){
        double data = new Double("1310") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }
    else if (result > 1310){
        double data = new Double("1400") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }
    else if (result > 1400){
        double data = new Double("1450") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }
    else if (result > 1450){
        double data = new Double("1505") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }
    else if (result > 1505){
        double data = new Double("1555") - new Double(folderName); 
        Datum.setText(Double.toString(data));
    }
    else if (result > 1555){
        double data = new Double("1645") - new Double(folderName); 
        Datum.setText(Double.toString(data));

    }
}
}

I want it to work like this: if the time is for example 14:01. (1401) then it needs to make a calculation 1450 minus the current time (1401). and display the output (49) in a textview.

My textview does not display any output. how do i solve this?

2 Answers 2

3

You should convert the Strings to time objects. Substract these two times and convert the result to a String.

java.text.DateFormat df = new java.text.SimpleDateFormat("hh:mm");
java.util.Date date1 = df.parse("14:01");
java.util.Date date2 = df.parse("14:50");
long diff = date2.getTime() - date1.getTime();

Diff will be the time difference in milliseconds.

Sign up to request clarification or add additional context in comments.

Comments

1

The String.compareTo(String otherString) method compares two string lexicographically, according to the javadocs, and it is mostly used to order lists. What you need is what @RvdK made, but I would use the absolute value from date1 - date2, because maybe the date2 was after date1:

Math.abs(date2.getTime() - date1.getTime());

Could not post this as comment because of the point system :/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.