How to round time to nearest 15 minute in java

跟風遠走 提交于 2019-12-24 09:29:00

问题


My program needs to calculate the overtime periods of workers for salary calculation purposes. To this end, what I am doing now is getting the worker's clock-in time and out time, then I calculate with the company's time scales and get how many hours overtime each worker has done. I need to round these times to the nearest 15 minutes, so I need a class to pass in the hours and return the rounded hours.

For example:

  • if the overtime hours is 2.17 hrs it must be rounded to 2.15.
  • if the overtime hours is 2.24 hrs it must be rounded to 2.30.
  • if the overtime hours is 2.40 hrs it must be 2.45.
  • if the overtime hours is 2.56 hrs it must 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 "";
    

    }


回答1:


If you just can get minutes extracted from the time and pass it 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 number greater than or equal to 8 is near to 15.




回答2:


Example that uses 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




回答3:


Well, rounding is easy to do with a modulo operation.

Here you want to round to a 15minutes.

Using the epoch time in milliseconds, you just need to remove the remainder of the modulo operation like this :

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


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

Tue Jan 03 09:35:56 CET 2017

Tue Jan 03 09:30:00 CET 2017

15*60*1000 is simply the number of milliseconds in 15minutes

You just need to check the value of the remainder to add 15minute if need. So just store the remainder :

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

Then substract it to the current time

l -= r;

And check the number of minute (depends on the precision you really wants.

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

This will round up if the quarter is past from 8minutes (if this 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;


来源:https://stackoverflow.com/questions/41438768/how-to-round-time-to-nearest-15-minute-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!