如何使用SimpleDateFormat和显示通知比较存储的日期和时间?

时间:2017-11-17 09:36:39

标签: java android nullpointerexception simpledateformat

在我的应用程序中,我想根据用户已设置的时间显示通知,并将日期和时间存储到本地数据库中。
我从本地数据库获取日期和时间,并与 SimpleDateFormat 进行比较,并使用日期解析,如此

SimpleDateFormat yourDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
Date date = null;
     try { 
          String date_time = "17-11-2017 15:10";
          date = yourDateFormat.parse(date_time);

     } catch (ParseException e) {
          Log.e(TAG, "Parsing date time failed", e);
     }
     Calendar calendar = Calendar.getInstance();
     calendar.setTime(date);

     Calendar current = Calendar.getInstance();
     if (calendar.compareTo(current) <= 0) {

        Toast.makeText(getApplicationContext(), "Invalid Date/Time", Toast.LENGTH_LONG).show();
     } else {
          AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
          Intent myIntent = new Intent(RemainderActivity.this, NotificationReceiver.class);             
          PendingIntent displayIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0);
          alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), displayIntent);

    }


有些设备工作正常,如7.0以上Android版本但低于7.0版本,它显示java.lang.NullPointerException像这样

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Date java.text.SimpleDateFormat.parse(java.lang.String)' on a null object reference


并且错误显示在date = yourDateFormat.parse(date_time);

请帮帮我。
在此先感谢。

3 个答案:

答案 0 :(得分:0)

好的,问题在这里改变了你的新解决方案:

在此之前:

Calendar calendar = Calendar.getInstance();
     calendar.setTime(date);

添加:

Locale locale = new Locale("en","US")

然后替换:

Calendar calendar = Calendar.getInstance();

使用:

Calendar calendar = Calendar.getInstance(locale);

所以它看起来像这样:

Locale locale = new Locale("en","US")
Calendar calendar = Calendar.getInstance(locale);
calendar.setTime(date);

您提供的全部代码如下:

SimpleDateFormat yourDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
Date date = null;
 try { 
      String date_time = "17-11-2017 15:10";
      date = yourDateFormat.parse(date_time);

 } catch (ParseException e) {
      Log.e(TAG, "Parsing date time failed", e);
 }
 Locale locale = new Locale("en","US")
 Calendar calendar = Calendar.getInstance(locale);
 calendar.setTime(date);

 Calendar current = Calendar.getInstance();
 if (calendar.compareTo(current) <= 0) {

    Toast.makeText(getApplicationContext(), "Invalid Date/Time", Toast.LENGTH_LONG).show();
 } else {
      AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      Intent myIntent = new Intent(RemainderActivity.this, NotificationReceiver.class);             
      PendingIntent displayIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0);
      alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), displayIntent);

}

答案 1 :(得分:0)

要获取用户选择的日期,您可以使用此

private void getSelectedDate() {
        String myFormat = "dd/MM/yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        String new_date = sdf.format(cal.getTime());
        System.out.println("DATE SELECTED IS ::    " + new_date);
        your_textview.setText(new_date);
    }

答案 2 :(得分:0)

这是很多代码,只需几行代码即可设置闹钟。您需要做的是将日期作为毫秒DatePicker或其他API,并将AlarmManager配置为在该时间到期时触发。像这样:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
          Intent myIntent = new Intent(RemainderActivity.this, NotificationReceiver.class);             
          PendingIntent displayIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0);
          alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime() +
        60000, alarmIntent);

以上代码每隔1分钟(60000毫秒)触发BroadcastReceiver。只需用用户设置日期替换时间即可。