2017-11-17 4 views
0

내 응용 프로그램에서 사용자가 이미 설정 한 시간을 기준으로 알림을 표시하고 날짜 및 시간을 로컬 데이터베이스에 저장하려고합니다.
나는 7.0 안드로이드 버전 이상하지만 7.0을 아래와 같이 잘 작동되는 일부 장치를 로컬 데이터베이스에서 날짜 및 시간을 받고 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); 

    } 


처럼 날짜 구문 분석하고 버전은이

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

확인 후 문제가 새로운 솔루션 여기에 변경이 전에

:

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

} 
+0

친구가이 게시물을 답변으로 표시하는 것을 잊지 마세요. 해피 코딩 :) –

+0

버디, 누구에게나 작동한다면, 우리는 여전히 왜 그 이유를 모르겠다. :-) 이것이 OP 문제를 해결할 수 있다고 생각하는 이유와 이유에 대해 설명해주십시오. 그래서 우리 모두는 배울지도 모른다. 코드 만 응답하면 도움이되지 않습니다. 고맙습니다. –

+0

@ M.SaadLakhan 죄송합니다, Typo wrong. 이 메서드는'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

많은 코드이므로 알람을 설정하면됩니다. 몇 줄의 코드 만 있으면됩니다. 당신이해야 할 일은 밀리 세컨드 (milliseconds) 인 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를 실행합니다. 시간을 사용자 설정 날짜로 바꾸십시오.

관련 문제