Get the specific 15 minutes timeframe based on current time

Question

I would like to get the 15 minutes timeframe based on Current Time.

For example:

Timeframe is for every 15 minutes.

i.e 12 to 1 is divided into four timeframes [ 12:00 to 12:15, 12:16 to 12:30, 1:31 to 12:45, 12:46 to 1:00]

If the current time is 12:34 means, i need to return the timeframe as 12:31 to 12:45

Is it something we can do easily with Java 8 Date and Time API?


Show source
| java   | java-8   | java-time   2017-01-03 09:01 2 Answers

Answers ( 2 )

  1. 2017-01-03 09:01

    You could create a TemporalAdjuster that calculates the end of the current 15-minute period and calculate the start of the period by removing 14 minutes.

    Could look like this:

    public static void main(String[] args) {
      LocalTime t = LocalTime.of(12, 34);
      LocalTime next15 = t.with(next15Minute());
      System.out.println(next15.minusMinutes(14) + " - " + next15);
    }
    
    public static TemporalAdjuster next15Minute() {
      return (temporal) -> {
        int minute = temporal.get(ChronoField.MINUTE_OF_DAY);
        int next15 = (minute / 15 + 1) * 15;
        return temporal.with(ChronoField.NANO_OF_DAY, 0).plus(next15, ChronoUnit.MINUTES);
      };
    }
    

    which outputs 12:31 - 12-45.

    Note: I'm not sure how it behaves around DST changes - to be tested.

  2. 2017-01-03 09:01

    You can simply use the Date API and calculate the intervals. Divide with your interval (15 min) and multiply again. This will strip off the minutes and round to the lower 15 minutes interval. Now you have to add fifteen minutes to get your needed output. See the following code:

    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    class Ideone
    {
        public static void main (String[] args) throws java.lang.Exception
        {
            long ms = new Date().getTime();
            System.out.println("Current time: " + new Date().toString());
    
            long fifteen = 15 * 60 * 1000;
            long newMs = (ms / fifteen) * fifteen + fifteen;
            System.out.println("Calculated time: " + new Date(newMs));
        }
    }
    

    See running example

    EDIT:

    Running example with LocalDate

    import java.util.*;
    import java.lang.*;
    import java.io.*;
    import java.time.*;
    import java.time.temporal.ChronoField;
    import java.time.temporal.ChronoUnit;
    
    class Ideone
    {
        public static void main (String[] args) throws java.lang.Exception
        {
            LocalTime now = LocalTime.now();
            System.out.println(now);
    
            LocalTime next = now.with((temp) -> {
                int currentMinute = temp.get(ChronoField.MINUTE_OF_DAY);
                int interval = (currentMinute / 15) * 15 + 15;
                temp = temp.with(ChronoField.SECOND_OF_MINUTE, 0);
                temp = temp.with(ChronoField.MILLI_OF_SECOND, 0);
                return temp.with(ChronoField.MINUTE_OF_DAY, interval);  
            });
            System.out.println(next);
        }
    }
    
◀ Go back