How to round time to the nearest 15 minutes in java

My program should calculate overtime periods for employees for payroll purposes. To this end, what I am doing now is to obtain working time — time and time, and then I calculate using the company's time scales and get how many hours each worker has worked overtime. I need to round these points to the next 15 minutes, so I need to go through the class in hours and return to round hours.

For instance:

  • if overtime is 2.17 hours, it should be rounded to 2.15.
  • if overtime hours are 2.24 hours, rounding should be rounded to 2.30.
  • if overtime is 2.40 hours, it should be 2.45.
  • if overtime is 2.56 hours, it should be 3 hours.

    public String RoundToNearest15Min (long time) {

    long milsec = Time;
    System.out.println("milsecond :" + milsec);
    long sec = Time / 1000;
    ValidateValues.roundupDouble(sec);
    System.out.println("second :" + sec);
    double min = sec / 60;
    ValidateValues.roundupDouble(min);
    System.out.println("miniutes :" + min);
    double hrs = min / 60;
    System.out.println("hours :" + hrs);
    double roundeVal = ValidateValues.roundupDouble(hrs);
    String hrsvalue = String.valueOf(roundeVal);
    System.out.println(hrsvalue);
    
    String splitVal[] = hrsvalue.split("\\.");
    System.out.println(splitVal[0]);
    System.out.println(splitVal[1]);
    int splitedValue2 = Integer.parseInt(splitVal[1]);
    int splitedValue1 = Integer.parseInt(splitVal[0]);
    
    if (splitedValue2 <= 15) {
        int rounded = splitedValue2 > 7 ? (15) : (0);
        return splitedValue1 + "." + rounded;
    }
    if (splitedValue2 > 15 && splitedValue2 <= 30) {
        int rounded = splitedValue2 > 22 ? (30) : (15);
        return splitedValue1 + "." + rounded;
    }
    if (splitedValue2 > 30 && splitedValue2 <= 45) {
        int rounded = splitedValue2 > 37 ? (45) : (30);
        return splitedValue1 + "." + rounded;
    }
    if (splitedValue2 > 45 && splitedValue2 <= 59) {
        int rounded = splitedValue2 > 51 ? (00) : (45);
        if (rounded == 00) {
            splitedValue1++;
            return splitedValue1 + "." + rounded;
        } else {
            return splitedValue1 + "." + rounded;
        }
    }
    return "";
    

    }

0
source share
3 answers

If you can just extract the minutes from the time and pass them to this function:

public static int getNear15Minute(int minutes){
        int mod = minutes%15; 
        int res = 0 ;
        if((mod) >=8){
            res = minutes+(15 - mod);
        }else{
            res = minutes-mod;
        }
        return res; //return rounded minutes
    }

here I assumed that a number greater than or equal to 8 is close to 15.

+1
source

An example that uses the current Date

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

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int round = calendar.get(Calendar.MINUTE) % 15;
        calendar.add(Calendar.MINUTE, round < 8 ? -round : (15-round));
        calendar.set( Calendar.SECOND, 0 );
        System.out.println(calendar.getTime());
    }
}

Test

Tue Jan 03 09:15:00 CET 2017

Online demo

0
source

, .

15 .

, :

long l = System.currentTimeMillis();
l -= l % (15*60*1000);


System.out.println(new Date(System.currentTimeMillis()));
System.out.println(new Date(l));

03 09:35:56 CET 2017

03 09:30:00 CET 2017

15*60*1000 - 15

, 15minute . :

long r = l % (15*60*1000);

l -= r;

And check the number of minutes (depends on the accuracy you really want.

if( r / 60_000 >= 8)
     l += 15*60*1000;

This will be rounded if a quarter has passed since 8 minutes (if it really means anything in English;))

Full code:

long l = System.currentTimeMillis();
long r = l % (15*60*1000);
l -= r;
if( r / 60_000 >= 8)
     l += 15*60*1000;
0
source

Source: https://habr.com/ru/post/1665584/


All Articles