Rounding a time to its lowest quarter-hour

Jyotishgher Astrology
By -
0

Rounding a time to its lowest quarter-hour


Suppose we want Time in round off Such as We select 9:25 its must show nearest Hours 
10:00 then we need to do some modifications as mentioned below


Approach 1


The division will round the number of 15-minute intervals down to the nearest integer, and multiplying it back gets you the interval you want.

Java has integer division, so you can divide the minutes by 15 and multiply them again:
int mins = calendar.get(Calendar.MINUTE);
calendar.set(Calendar.MINUTE, mins / 15 * 15);
 Approach 2

Calendar datetime = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
String currentdate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());

int mode = minute % 15 * 15;
if (mode > 15 / 2) {
    minute = 15 - mode;
    Log.d("minute", "onTimeSet: " +minute);
} else {
    minute = 0 - mode;
    Log.d("minute1", "onTimeSet: " +minute);
}

if (minute < 15) {
    minute = 0;
} else if (minute < 30) {
    minute = 15;
} else if (minute < 45) {
    minute = 30;
} else {
    minute = 45;
}



BELOW I AM MENTIONING A LOGIC WHICH WILL HELP TO PICKUP TIME IN AM AND PM FORMAT AND 
THE TIME WILL BE AVAILABLE IN NEAREST TIME NOT IN PAST TIME:

THis must be written on Click action 

In java how do we round off time to nearest hour and minute?


final Calendar c = Calendar.getInstance();mHour = c.get(Calendar.HOUR_OF_DAY);mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker DialogTimePickerDialog timePickerDialog = new TimePickerDialog(hotel_details.this,
        new TimePickerDialog.OnTimeSetListener() {

            @Override            public void onTimeSet(TimePicker view, int hourOfDay,
                                  int minute) {
                Calendar datetime = Calendar.getInstance();
                Calendar calendar = Calendar.getInstance();
                String currentdate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());

                int mode = minute % 15 * 15;
                if (mode > 15 / 2) {
                    minute = 15 - mode;
                    Log.d("minute", "onTimeSet: " +minute);
                } else {
                    minute = 0 - mode;
                    Log.d("minute1", "onTimeSet: " +minute);
                }

                if (minute < 15) {
                    minute = 0;
                } else if (minute < 30) {
                    minute = 15;
                } else if (minute < 45) {
                    minute = 30;
                } else {
                    minute = 45;
                }

                if (currentdate.equalsIgnoreCase(CHECKINDATE)) {

                    datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);

                    datetime.set(Calendar.MINUTE, minute);
                    datetime.set(Calendar.MILLISECOND, 0);

                    if (datetime.getTimeInMillis() >= calendar.getTimeInMillis()) {
                        // hour = hourOfDay % 12;                        hour = hourOfDay;

                        if (hourOfDay < 10 && minute < 10) {
                            // checkintime.setText("0" + hour + ":0" + minute);

                            CHECKINTIME = "0" + hour + ":0" + minute;
                            final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                            final Date dateObj;
                            try {
                                dateObj = sdf.parse(CHECKINTIME);
                                checkintime.setText(new SimpleDateFormat("hh:mm a").format(dateObj));


                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            calculateOut(CHECKINTIME + ":" + 30);
                        } else {
                            //checkintime.setText(hour + ":" + minute);                            CHECKINTIME = hour + ":" + minute;
                            final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                            final Date dateObj;
                            try {
                                dateObj = sdf.parse(CHECKINTIME);
                                checkintime.setText(new SimpleDateFormat("hh:mm a").format(dateObj));

                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            calculateOut(CHECKINTIME + ":" + 30);

                        }
                    } else {


                        Toast.makeText(hotel_details.this, "Please select Time in nearest hours", Toast.LENGTH_LONG).show();



                    }
                } else {
                    if (hourOfDay < 10 && minute < 10) {
                        //  checkintime.setText("0" + hour + ":0" + minute);                        CHECKINTIME = "0" + hour + ":0" + minute;

                        final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                        final Date dateObj;
                        try {
                            dateObj = sdf.parse(CHECKINTIME);
                            checkintime.setText(new SimpleDateFormat("hh:mm a").format(dateObj));

                        } catch (ParseException e) {
                            e.printStackTrace();
                        }


                        calculateOut(CHECKINTIME + ":" + 30);
                    } else {


                        // checkintime.setText(hour + ":" + minute);                        CHECKINTIME = hour + ":" + minute;

                        final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                        final Date dateObj;
                        try {
                            dateObj = sdf.parse(CHECKINTIME);
                            checkintime.setText(new SimpleDateFormat("hh:mm a").format(dateObj));

                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        calculateOut(CHECKINTIME + ":" + 30);

                    }
                }


            }
        }, mHour, mMinute, false);
timePickerDialog.show();



APPROACH 3

For calendar operations you have to use Calendar class. 
In your case you would do something like this:


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TestDate {

    public static void main(String[] args) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.HOUR_OF_DAY, 12);
        c.set(Calendar.MINUTE, 58);
        c.set(Calendar.SECOND, 15);
        Date d = c.getTime();

        System.out.println("Start point: " + d.toString());
        System.out.println("Nearest whole minute: " + toNearestWholeMinute(d));
        System.out.println("Nearest whole hour: " + toNearestWholeHour(d));
    }

    static Date toNearestWholeMinute(Date d) {
        Calendar c = new GregorianCalendar();
        c.setTime(d);

        if (c.get(Calendar.SECOND) >= 30)
            c.add(Calendar.MINUTE, 1);

        c.set(Calendar.SECOND, 0);

        return c.getTime();
    }

    static Date toNearestWholeHour(Date d) {
        Calendar c = new GregorianCalendar();
        c.setTime(d);

        if (c.get(Calendar.MINUTE) >= 30)
            c.add(Calendar.HOUR, 1);

        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        return c.getTime();
    }

}

And the result:
Start point: Tue Nov 22 12:58:15 CET 2019
Nearest whole minute: Tue Nov 22 12:58:00 CET 2019
Nearest whole hour: Tue Nov 22 13:00:00 CET 2019

Post a Comment

0Comments

Post a Comment (0)