0

我有这个代码

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));

    }
}
}

我希望它像这样工作:如果时间是例如 14:01。(1401) 然后它需要计算 1450 减去当前时间 (1401)。并在文本视图中显示输出 (49)。

我的 textview 不显示任何输出。我该如何解决这个问题?

4

2 回答 2

3

您应该将字符串转换为时间对象。减去这两次并将结果转换为字符串。

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 将以毫秒为单位的时间差。

于 2013-01-26T14:29:12.703 回答
1

根据 javadocs,String.compareTo(String otherString) 方法按字典顺序比较两个字符串,它主要用于对列表进行排序。您需要的是 @RvdK 所做的,但我会使用 date1 - date2 的绝对值,因为 date2 可能在 date1 之后:

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

由于积分系统,无法将此作为评论发布:/

于 2013-01-26T15:11:14.300 回答