Hirak Chhatbar
Hirak Chhatbar

Reputation: 3181

get days between 2 dates android

Update

The alarm service is not getting on at selected date and time.

add_task_frag (fragment that triggers alarm) :

Intent intent = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getService(getActivity(), 0, intent, 0);
AlarmManager am = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, 5000, pendingIntent);

The alarm should go on after 5 secids but it doesnt happen. if i put this code (for pending intent)

PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);

then alarm goes on immediately after button click instead of after 5 seconds.

Below is alarm reciever class :

super.onCreate(savedInstanceState);
    PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Wake Lock");
    wakeLock.acquire();
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.alarm);

    Button stopalarm = (Button)findViewById(R.id.stop);
    stopalarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaPlayer.stop();
            finish();
        }
    });
    playSound (this, getAlarmUri());
}
private void playSound (Context context, Uri alert){
    mediaPlayer = new MediaPlayer();
    try{
        mediaPlayer.setDataSource(context, alert);
        final AudioManager audioManager = (AudioManager)context.getSystemService(AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0){
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mediaPlayer.prepare();
            mediaPlayer.start();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private Uri getAlarmUri(){
    Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alert == null){
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (alert == null){
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        }
    }
    return alert;
}

@Override
protected void onStop() {
    super.onStop();
    wakeLock.release();
}

I want no. of days between Today's date and the date selected by user.

I have an alarm kind of app in where user will set reminder at particular date. Is there a way to set alarm at user selected date or I have to calculate number of days between the 2 dates and then convert them into milliseconds?

This is where user selects time:

    Calendar calender = Calendar.getInstance();
    calender.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calender.set(Calendar.MINUTE, minute);
    timePicker.setIs24HourView(false);
    SimpleDateFormat sdf = new SimpleDateFormat("hh : mm a");
    timetv.setText("@ " + sdf.format(calender.getTime()));

This is where user selects date :

    calender.set(Calendar.YEAR, year);
    calender.set(Calendar.MONTH, month);
    calender.set(Calendar.DAY_OF_MONTH, day);
    SimpleDateFormat sdf = new SimpleDateFormat("EE, d MMM yyy");
    datetv.setText("On " + sdf.format(calender.getTime()));

Now how to set reminder at this particular date & time which user selected.

Upvotes: 0

Views: 96

Answers (3)

himanshu
himanshu

Reputation: 21

Well to start with, you should only deal with them as strings when you have to. Most of the time you should work with them in a data type which actually describes the data you're working with.

Here is your solution

Upvotes: 0

Zsolt Boldizsar
Zsolt Boldizsar

Reputation: 2582

If you use one single instance of Calendar than call:

calendar.getTimeInMillis()

This will return the time in milliseconds right what your user selected. Furthermore this can be used to trigger an alarm using AlarmManager.

Upvotes: 0

duggu
duggu

Reputation: 38439

try below code:-

SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
String inputString1 = "23 01 1997";
String inputString2 = "27 04 1997";

try {
    Date date1 = myFormat.parse(inputString1);
    Date date2 = myFormat.parse(inputString2);
    long diff = date2.getTime() - date1.getTime();
    System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
    e.printStackTrace();
}

see below link for more info:-

http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java/

Upvotes: 1

Related Questions