0

我正在尝试根据用户输入的日期在我的 android 应用程序中设置倒计时。

因此,用户有一个日期选择器,它在 EditText 对象中设置日期,然后当我单击保存时,我需要它从 EditText 对象中获取值并运行以下代码:

final EditText dateField = (EditText) findViewById(R.id.dateSentData);  
        String dateSent = dateField.getText().toString();
        Double date = Double.parseDouble(dateSent);

        Time TimerSet = new Time();
        TimerSet.set(date); //day month year
        TimerSet.normalize(true);
        long millis = TimerSet.toMillis(true);

        Time TimeNow = new Time();
        TimeNow.setToNow(); // set the date to Current Time
        TimeNow.normalize(true);
        long millis2 = TimeNow.toMillis(true);

        long millisset = millis - millis2; //subtract current from future to set the time remaining

        final int smillis = (int) (millis); //convert long to integer to display conversion results
        final int smillis2 = (int) (millis2);

new CountDownTimer(millisset, 1000) {
            public void onTick(long millisUntilFinished) {

                mText = (TextView)findViewById(R.id.weddingDateData);

                // decompose difference into days, hours, minutes and seconds 
                int weeks = (int) ((millisUntilFinished / 1000) /
                        604800);
                int days = (int) ((millisUntilFinished / 1000) / 86400);
                int hours = (int) (((millisUntilFinished / 1000) - (days
                        * 86400)) / 3600);
                int minutes = (int) (((millisUntilFinished / 1000) - ((days
                        * 86400) + (hours * 3600))) / 60);
                int seconds = (int) ((millisUntilFinished / 1000) % 60);
                int millicn = (int) (millisUntilFinished / 1000);

                mText.setText(" " +days);
            }
            public void onFinish() {

            }
        }.start();

上面的代码不会运行,因为它在下面的行中除了 String 或 Double 只有 Long:

TimerSet.set(date);

有人可以帮我吗?

提前致谢

4

2 回答 2

1

嗨,您可以设置更改日期和时间

TimerSet.set(date); 

有了这个

//TimerSet.set(int second, int minute, int hour, int monthDay, int month, int year) This is the structure for method TimerSet
TimerSet.set(0,25,24,12,7,2013);

所以日期和时间应该是 12/07/2013 24:25:00

但是我认为日期有问题,当我用 TimerSet 发送祝酒词时

Toast.makeText(MainActivity.this, "date: "+ TimerSet, Toast.LENGTH_LONG).show(); 

它说我设置的日期是 13/08/2013 24:25:00

所以我所做的就是改变这个

Time TimerSet = new Time();
TimerSet.set(0,25,24,12,7,2013);

有了这个(请注意,我将月份和日期减 1)

int year = 2013;
int month = 7 - 1;
int day = 12 - 1;
int hour = 24;
int minutes = 55;
int seconds = 0;

Time TimerSet = new Time();
//set(int second, int minute, int hour, int monthDay, int month, int year)
TimerSet.set(seconds,minutes,hour,day,month,year);

有了这个,它给了我正确的日期 12/07/2013 24:25:00

希望这对您有所帮助,或者如果您有其他解决方案,请告诉我如何通过其他方式解决。

此致

于 2013-07-12T06:04:43.990 回答
0

该方法Time.set需要一个 long 值,表示自epoch以来的毫秒数。

您必须以另一种方式解析日期,具体取决于您收到的输入日期格式。

于 2013-07-07T18:11:05.087 回答