如何在android中比较用户输入时间和当前时间?

时间:2012-09-17 14:26:23

标签: android time timer

如何比较当前时间和用户输入时间,并通过意图调用另一个类? 我正在制作一个警报类应用程序,我使用时间戳来获取代码在这里的用户输入时间:

    private TimePickerDialog.OnTimeSetListener theTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hour, int minute) {
      String time = "User picked Hour: " + String.valueOf(hour) + "\n"
                        + "Minute: " + String.valueOf(minute);
      Toast.makeText(MainActivity.this, time, Toast.LENGTH_LONG).show();
      int theHour = hour;
      int theMinute = minute;

      Intent alarmIntent = new Intent(MainActivity.this, MyAlarmService.class);
      alarmIntent.putExtra("nel.example.MainActivity","My message");
      PendingIntent pendingAlarmIntent = PendingIntent.getService(MainActivity.this, 0,
                                   alarmIntent, 0);

      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

      Calendar AlarmCal = Calendar.getInstance();
      AlarmCal.setTimeInMillis(System.currentTimeMillis());
      AlarmCal.set(Calendar.HOUR_OF_DAY, theHour);  // set user selection
      AlarmCal.set(Calendar.MINUTE, theMinute);        // set user selection
      AlarmCal.set(Calendar.SECOND, 0);
      alarmManager.set(AlarmManager.RTC_WAKEUP, AlarmCal.getTimeInMillis(),
              pendingAlarmIntent);
   }
};

从这里我能够获得用户输入时间bt我无法比较时间并将意图发送给另一个类?

plz help !!!

提前thnx ......:)

1 个答案:

答案 0 :(得分:3)

当前时间

Date curDate = new Date();

用户输入:

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date userDate = df.parse("31/12/2011");

然后比较:

if(curDate.after(userDate)) { .... }