How to round time to the nearest quarter hour in java?

后端 未结 14 2632
小鲜肉
小鲜肉 2020-11-27 14:59

Given today\'s time e.g. 2:24PM, how do I get it to round to 2:30PM?

Similarly if the time was 2:17PM, how do I get it to round to 2:15PM?

相关标签:
14条回答
  • 2020-11-27 15:33

    if you have the minutes you can round them with the following function:
    int minutes = i % 15 < 8 ? i / 15 * 15 : (i / 15 + 1) * 15;

    0 讨论(0)
  • 2020-11-27 15:43

    Use the following functions to get the minutes rounded to last quarter getRecentQuater():Date, getSysDate_LastQuarterMins("dd.MM.yyyy HH:mm:ss"):String: Converting LocalDateTime to Date

    public static Date getRecentQuater() {
        LocalDateTime time = LocalDateTime.now();
        LocalDateTime lastQuarter = time.truncatedTo(ChronoUnit.HOURS).plusMinutes(getLastQuarterValue(time.getMinute()));
        System.out.println("lastQuarter LocalDateTime: " + lastQuarter);
    
        Date date = Date.from(lastQuarter.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("lastQuarter Date: " + lastQuarter);
        return date;
    }
    public static String getSysDate_LastQuarterMins(String dateFormat) {
        Date date = getRecentQuater();
        SimpleDateFormat ft = new SimpleDateFormat (dateFormat);
        String sysDate_RoundMin = ft.format(date);
        System.out.println("getSysDate_LastQuarterMins() LocalDateTime : "+sysDate_RoundMin);
        return sysDate_RoundMin;
    }
    

    getSysDate_LastQuarterMins() : Mon Jan 20 17:30:00 CET 2020

    public static Date getSysDate_LastQuarterMins() {
        Calendar cal = Calendar.getInstance();
        cal.setTime( new Date(System.currentTimeMillis()) );
    
        int min = cal.get(Calendar.MINUTE);
    
        cal.set(Calendar.MINUTE, getLastQuarterValue(min));
        cal.set(Calendar.SECOND, 00);
        Date lastQuarter = cal.getTime();
        System.out.println("getSysDate_LastQuarterMins() Calendar : "+lastQuarter);
        return lastQuarter;
    }
    

    You can find the LastQuarter Value Round value from the follwing fucntions, provided with some outputs on function call diaplayLastQuarter_RoundValue(min):

    Min: 10, LastQuarter:  0, Round: 15
    Min: 24, LastQuarter: 15, Round: 30
    Min: 36, LastQuarter: 30, Round: 30
    Min: 37, LastQuarter: 30, Round: 30
    Min: 38, LastQuarter: 30, Round: 45
    Min: 39, LastQuarter: 30, Round: 45
    Min: 44, LastQuarter: 30, Round: 45
    Min: 57, LastQuarter: 45, Round: 00 [57, 07:45:00, 08:00:00]
    
    public static void diaplayLastQuarter_RoundValue(int minutes) {
        System.out.format("Min: %2d, LastQuarter: %2d, Round: %2d\n",
                minutes, getLastQuarterValue(minutes), getRoundValue(minutes));
    }
    public static int getLastQuarterValue(int minutes) {
        int min = 15 * (minutes / 15);
        //System.out.println("Min: "+minutes+", getLastQuarterValue : "+ min);
        return min;
    }
    public static int getRoundValue(int minutes) {
        getLastQuarterValue(minutes);
        int minRound = (int) (Math.round(minutes / 15.0) * 15.0);
        //System.out.println("Min: "+minutes+", getRoundValue : "+minRound);
        return minRound;
    }
    
    0 讨论(0)
提交回复
热议问题