sanjay
sanjay

Reputation: 2620

How to get 10 min before & after time from a particular time in android

Im doing dateTimePicker app in android.On that i need to get 10 min before and after time for a particular dateTime.Ex. If user selects "12/12/2010 5:00", i need to get "12/12/2010 4:50" as one value & "12/12/2010 5:10" as one value.How could i do + 10min & -10min from a particular time?

My Code:

String DATETIMEBEFORE ;
String DATETIMEAFTER;
...
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int selectedHour,
                int selectedMinute) {
            hour = selectedHour;
            minute = selectedMinute;

            String DATETIME = new StringBuilder()
                    // Month is 0 based, just add 1
                    .append(month + 1).append("-").append(day).append("-")
                    .append(year).append(" ").append(pad(hour)).append(":")
                    .append(pad(minute)).toString();

If DATETIME has value="12/12/2010 5:00".i need to get DATETIMEBEFORE value="12/12/2010 4:50" & DATETIMEAFTER value="12/12/2010 5:10". Thanks.

Upvotes: 0

Views: 2415

Answers (2)

Pankaj Kumar
Pankaj Kumar

Reputation: 82958

Update after OPs code

public static String getTimeBefore10minutes(String currentDate) {

        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        try {
            // Get calendar set to current date and time with Singapore time zone
            Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Asia/Calcutta"));
            calendar.setTime(format.parse(currentDate));

            //Set calendar before 10 minutes
            calendar.add(Calendar.MINUTE, -10);     
            //Formatter 
            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            formatter.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));  
            return formatter.format(calendar.getTime());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public static String getTimeAfter10minutes(String currentDate) {

        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        try {
            // Get calendar set to current date and time with Singapore time zone
            Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Asia/Calcutta"));
            calendar.setTime(format.parse(currentDate));

            //Set calendar before 10 minutes
            calendar.add(Calendar.MINUTE, 10);     
            //Formatter 
            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            formatter.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));  
            return formatter.format(calendar.getTime());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

And call like

getTimeAfter10minutes("12/12/2010 5:00");
getTimeBefore10minutes("12/12/2010 5:00");

Result will be

12/12/2010 05:10
12/12/2010 04:50

Read more about my blog Calculate Date Ranges

Upvotes: 1

Neoh
Neoh

Reputation: 16164

If you have the selected time in milis,

long now = System.currentTimeMillis(); // or any time value in miliseconds
long tenMinuteBefore = now - 600000;
long tenMinuteAfter  = now + 600000;

Upvotes: 0

Related Questions