2017-11-17 13 views
0

私のアプリケーションでは、ユーザーが既に設定した時刻に基づいて通知を表示したいし、日付と時刻をローカルデータベースに格納しています。
私はローカルデータベースから日付と時刻を取得し、SimpleDateFormatのと比較すると、それは罰金7.0アンドロイドバージョンを超えるが7.0以下のように働いている 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); 

    } 



このようないくつかのデバイスで解析していますバージョンは、この

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 

のようなjava.lang.NullPointerExceptionを示しています このエラーはここに表示されますdate = yourDateFormat.parse(date_time);

私を助けてください。
ありがとうございました。

+0

この行をチェックしてください年が間違っていますString date_time = "17/11/207 15:10"; –

+0

申し訳ありませんが、間違って入力してください。元の形式は 'String date_time =" 17-11-2017 15:10 ";' – Ram

+0

あなたはその行から 'NullPointerException'を得ることができると信じています。これがあなたが実行している正確なコードだと確信していますか? –

答えて

0

[OK]を、その後、問題はここで変更された新しいソリューションです。この前

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

あなたが提供するあなたの全体のコードは次のようになります。ユーザーはuがこれを使用することができ、選択した日付を取得するには

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

} 
+0

これはあなたのために働く場合バディは、答えとしてこの投稿をマークすることを忘れないでください。ハッピーコーディング:) –

+0

バディ、それが誰のために働く場合、我々はまだなぜ、なぜ理解していない。 :-)これがOPの問題を解決する方法と理由を説明してください。だから我々は皆学ぶかもしれない。コードのみの回答は役に立ちません。ありがとうございました。 –

+0

@ M.SaadLakhan申し訳ありませんが、誤植は間違っています。私はこのメソッドを 'new SimpleDateFormat(" 17-11-2017 15:10 ");'のみ使っています。 – Ram

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); 
    } 
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を起動します。ちょうどその時間をユーザーが設定した日付に置き換えてください。

関連する問題