0

我是 android 新手,正在尝试开发一个简单的应用程序,用户将在其中输入两个日期,然后单击一个按钮,他将在几天内收到它们之间的差异。这是我的代码,不幸的是输出始终为 0。

package il.ac.hit.datetodate;

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

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        final EditText ed1 = (EditText) findViewById(R.id.editText1);
        final EditText ed2 = (EditText) findViewById(R.id.editText2);
        final TextView tv = (TextView) findViewById(R.id.textView2);
        final Button bt = (Button) findViewById(R.id.button1);

        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Calendar cal1 = Calendar.getInstance();
                Calendar cal2 = Calendar.getInstance();
                SimpleDateFormat sdf1 = new SimpleDateFormat("MMM dd yyyy");
                SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd yyyy");

                try {
                    cal1.setTime(sdf1.parse(ed1.getText().toString()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                try {
                    cal2.setTime(sdf2.parse(ed2.getText().toString()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                long days1 = cal1.getTimeInMillis();
                long days2 = cal2.getTimeInMillis();
                long diff = (days2 - days1)/(1000*60*60*24);
                tv.setText(String.valueOf(diff));
            }
        });
    }
}
4

4 回答 4

0

使用 SimpleDateFormat,您可以将字符串解析为日历对象:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done
于 2013-09-06T11:56:56.857 回答
0

试试这个代码,

long diff = date1.getTime() - date2.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
于 2013-09-06T11:58:20.723 回答
0

使用下面的代码。

 Calendar thatDay = Calendar.getInstance();
 thatDay.set(Calendar.DAY_OF_MONTH,25);
 thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
 thatDay.set(Calendar.YEAR, 1985);

 Calendar today = Calendar.getInstance();

 long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

一个近似...

long days = diff / (24 * 60 * 60 * 1000);

要从字符串中解析日期,您可以使用

String strThatDay = "1985/08/25";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date d = null;
try {
   d = formatter.parse(strThatDay);//catch exception
} catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} 
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(d); //rest is the same....

您还可以对其 Substrings 执行 Integer.parseInt() 以获得它们的数值。

于 2013-09-06T12:05:29.500 回答
0

看看这是否有效

If you want the difference between two times you can use this ::

Date date1 = cal.getTime(); String endTime = "2013/29/08 18:59:59"; 
SimpleDateFormat dateFormat =     new SimpleDateFormat( "yyyy/dd/MM HH:mm:ss"); 
Date dtEndTime = dateFormat.parse(endTime); 
DecimalFormat formatter = new DecimalFormat("###,###"); 
long diff = dtEndTime.getTime() - date1.getTime();

差异会给你以毫秒为单位的差异。

于 2013-09-06T13:11:18.123 回答