JAVA : Generate 15 Minute Time Interval AM/PM


In this program you can get list of in time interval with fixed difference Ex: 15 minute in AM and PM format.

Example

import java.util.ArrayList;
import java.util.List;

public class GenerateTimeInterval {

public static void main(String[] args) {
	int interval = 15; //minutes interval
	List timesList=new ArrayList() ; // time array
	String []ap = {"AM", "PM"}; // AM-PM
	String timeFormat;
	for(int h=0;h<24;h++)
	{
	for(int m=0;m<60;)
	{
		if(h<12)
		{
		timeFormat=String.format("%02d:%02d %s", h,m,"AM");
		}
		else
		{
		timeFormat=String.format("%02d:%02d %s", h,m,"PM");
		}
		timesList.add(timeFormat);
		m=m+interval;
		}

	}
	//To match the time
	timeFormat=String.format("%02d:%02d %s", 0,0,"AM");
	timesList.add(timeFormat);
	//print timings
	for(String time : timesList)
	{
		System.out.println(time);
	}

	}
}

Output


00:00 AM
00:15 AM
00:30 AM
00:45 AM
01:00 AM
01:15 AM
01:30 AM
01:45 AM
02:00 AM
02:15 AM
02:30 AM
02:45 AM
03:00 AM
03:15 AM
03:30 AM
03:45 AM
04:00 AM
04:15 AM
04:30 AM
04:45 AM
05:00 AM
05:15 AM
05:30 AM
05:45 AM
06:00 AM
06:15 AM
06:30 AM
06:45 AM
07:00 AM
07:15 AM
07:30 AM
07:45 AM
08:00 AM
08:15 AM
08:30 AM
08:45 AM
09:00 AM
09:15 AM
09:30 AM
09:45 AM
10:00 AM
10:15 AM
10:30 AM
10:45 AM
11:00 AM
11:15 AM
11:30 AM
11:45 AM
12:00 PM
12:15 PM
12:30 PM
12:45 PM
13:00 PM
13:15 PM
13:30 PM
13:45 PM
14:00 PM
14:15 PM
14:30 PM
14:45 PM
15:00 PM
15:15 PM
15:30 PM
15:45 PM
16:00 PM
16:15 PM
16:30 PM
16:45 PM
17:00 PM
17:15 PM
17:30 PM
17:45 PM
18:00 PM
18:15 PM
18:30 PM
18:45 PM
19:00 PM
19:15 PM
19:30 PM
19:45 PM
20:00 PM
20:15 PM
20:30 PM
20:45 PM
21:00 PM
21:15 PM
21:30 PM
21:45 PM
22:00 PM
22:15 PM
22:30 PM
22:45 PM
23:00 PM
23:15 PM
23:30 PM
23:45 PM
00:00 AM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s