• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

build dropdown list incrementing time in intervals of 5 mintues

I'm trying to build a dropdown list staring at 7:00 AM and ending at 8:00 PM.

So, the list would populate like this:

7:00 AM
7:05 AM
7:10 AM
7:15 AM
etc...
8:00 PM

I started with a for loop with the loop variable initialized at 7.

Any help is appreciated.

Thank you.


for(integer i = 7; i < 20; i++){
    strEndTime = i + ':00 AM';
    options.add(new SelectOption(strEndTime,strEndTime));
}

Open in new window

0
-Dman100-
Asked:
-Dman100-
  • 3
1 Solution
 
for_yanCommented:
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm am");
java.util.Date dd= sdf.parse("10-11-2011 8:00 AM", new ParsePosition(0));

Vector v = new Vector();
for(int j=0; j<132; j++){
java.util.Date dd1 = new java.utli.Date(dd.getTime() + 5L*60L*1000L);
String dateStr = sdf.format(dd1);
v.addElement(dateStr.substring(11));




}

JList list = new JList(v);

Open in new window

0
 
for_yanCommented:
This is now tested - use this code:

                      java.util.Date dd = null;
                  SimpleDateFormat sdf20 = new SimpleDateFormat("MMM-dd-yyyy hh:mm a");
        String dateS ="Oct-10-2011 7:00 AM";
        try{
 dd = sdf20.parse(dateS);

        } catch(Exception ex){
        ex.printStackTrace();
        }
      //  System.out.println(dd.getTime());

Vector v = new Vector();
for(int j=0; j<156; j++){
java.util.Date dd1 = new java.util.Date(dd.getTime() + 5L*60L*1000L*j);
String dateStr = sdf20.format(dd1);
v.addElement(dateStr.substring(11));




}
        System.out.println(v);

JList list1 = new JList(v);
        

Open in new window



Output:
[ 07:00 AM,  07:05 AM,  07:10 AM,  07:15 AM,  07:20 AM,  07:25 AM,  07:30 AM,  07:35 AM,  07:40 AM,  07:45 AM,  07:50 AM,  07:55 AM,  08:00 AM,  08:05 AM,  08:10 AM,  08:15 AM,  08:20 AM,  08:25 AM,  08:30 AM,  08:35 AM,  08:40 AM,  08:45 AM,  08:50 AM,  08:55 AM,  09:00 AM,  09:05 AM,  09:10 AM,  09:15 AM,  09:20 AM,  09:25 AM,  09:30 AM,  09:35 AM,  09:40 AM,  09:45 AM,  09:50 AM,  09:55 AM,  10:00 AM,  10:05 AM,  10:10 AM,  10:15 AM,  10:20 AM,  10:25 AM,  10:30 AM,  10:35 AM,  10:40 AM,  10:45 AM,  10:50 AM,  10:55 AM,  11:00 AM,  11:05 AM,  11:10 AM,  11:15 AM,  11:20 AM,  11:25 AM,  11:30 AM,  11:35 AM,  11:40 AM,  11:45 AM,  11:50 AM,  11:55 AM,  12:00 PM,  12:05 PM,  12:10 PM,  12:15 PM,  12:20 PM,  12:25 PM,  12:30 PM,  12:35 PM,  12:40 PM,  12:45 PM,  12:50 PM,  12:55 PM,  01:00 PM,  01:05 PM,  01:10 PM,  01:15 PM,  01:20 PM,  01:25 PM,  01:30 PM,  01:35 PM,  01:40 PM,  01:45 PM,  01:50 PM,  01:55 PM,  02:00 PM,  02:05 PM,  02:10 PM,  02:15 PM,  02:20 PM,  02:25 PM,  02:30 PM,  02:35 PM,  02:40 PM,  02:45 PM,  02:50 PM,  02:55 PM,  03:00 PM,  03:05 PM,  03:10 PM,  03:15 PM,  03:20 PM,  03:25 PM,  03:30 PM,  03:35 PM,  03:40 PM,  03:45 PM,  03:50 PM,  03:55 PM,  04:00 PM,  04:05 PM,  04:10 PM,  04:15 PM,  04:20 PM,  04:25 PM,  04:30 PM,  04:35 PM,  04:40 PM,  04:45 PM,  04:50 PM,  04:55 PM,  05:00 PM,  05:05 PM,  05:10 PM,  05:15 PM,  05:20 PM,  05:25 PM,  05:30 PM,  05:35 PM,  05:40 PM,  05:45 PM,  05:50 PM,  05:55 PM,  06:00 PM,  06:05 PM,  06:10 PM,  06:15 PM,  06:20 PM,  06:25 PM,  06:30 PM,  06:35 PM,  06:40 PM,  06:45 PM,  06:50 PM,  06:55 PM,  07:00 PM,  07:05 PM,  07:10 PM,  07:15 PM,  07:20 PM,  07:25 PM,  07:30 PM,  07:35 PM,  07:40 PM,  07:45 PM,  07:50 PM,  07:55 PM]

Open in new window

0
 
for_yanCommented:
This eleiminates extra space before time " 07:05" -> "07:05"
                      java.util.Date dd = null;
                  SimpleDateFormat sdf20 = new SimpleDateFormat("MMM-dd-yyyy hh:mm a");
        String dateS ="Oct-10-2011 7:00 AM";
        try{
 dd = sdf20.parse(dateS);

        } catch(Exception ex){
        ex.printStackTrace();
        }
      //  System.out.println(dd.getTime());

Vector v = new Vector();
for(int j=0; j<156; j++){
java.util.Date dd1 = new java.util.Date(dd.getTime() + 5L*60L*1000L*j);
String dateStr = sdf20.format(dd1);
v.addElement(dateStr.substring(12));




}
        System.out.println(v);

JList list1 = new JList(v);
        

Open in new window


Output:

[07:00 AM, 07:05 AM, 07:10 AM, 07:15 AM, 07:20 AM, 07:25 AM, 07:30 AM, 07:35 AM, 07:40 AM, 07:45 AM, 07:50 AM, 07:55 AM, 08:00 AM, 08:05 AM, 08:10 AM, 08:15 AM, 08:20 AM, 08:25 AM, 08:30 AM, 08:35 AM, 08:40 AM, 08:45 AM, 08:50 AM, 08:55 AM, 09:00 AM, 09:05 AM, 09:10 AM, 09:15 AM, 09:20 AM, 09:25 AM, 09:30 AM, 09:35 AM, 09:40 AM, 09:45 AM, 09:50 AM, 09:55 AM, 10:00 AM, 10:05 AM, 10:10 AM, 10:15 AM, 10:20 AM, 10:25 AM, 10:30 AM, 10:35 AM, 10:40 AM, 10:45 AM, 10:50 AM, 10:55 AM, 11:00 AM, 11:05 AM, 11:10 AM, 11:15 AM, 11:20 AM, 11:25 AM, 11:30 AM, 11:35 AM, 11:40 AM, 11:45 AM, 11:50 AM, 11:55 AM, 12:00 PM, 12:05 PM, 12:10 PM, 12:15 PM, 12:20 PM, 12:25 PM, 12:30 PM, 12:35 PM, 12:40 PM, 12:45 PM, 12:50 PM, 12:55 PM, 01:00 PM, 01:05 PM, 01:10 PM, 01:15 PM, 01:20 PM, 01:25 PM, 01:30 PM, 01:35 PM, 01:40 PM, 01:45 PM, 01:50 PM, 01:55 PM, 02:00 PM, 02:05 PM, 02:10 PM, 02:15 PM, 02:20 PM, 02:25 PM, 02:30 PM, 02:35 PM, 02:40 PM, 02:45 PM, 02:50 PM, 02:55 PM, 03:00 PM, 03:05 PM, 03:10 PM, 03:15 PM, 03:20 PM, 03:25 PM, 03:30 PM, 03:35 PM, 03:40 PM, 03:45 PM, 03:50 PM, 03:55 PM, 04:00 PM, 04:05 PM, 04:10 PM, 04:15 PM, 04:20 PM, 04:25 PM, 04:30 PM, 04:35 PM, 04:40 PM, 04:45 PM, 04:50 PM, 04:55 PM, 05:00 PM, 05:05 PM, 05:10 PM, 05:15 PM, 05:20 PM, 05:25 PM, 05:30 PM, 05:35 PM, 05:40 PM, 05:45 PM, 05:50 PM, 05:55 PM, 06:00 PM, 06:05 PM, 06:10 PM, 06:15 PM, 06:20 PM, 06:25 PM, 06:30 PM, 06:35 PM, 06:40 PM, 06:45 PM, 06:50 PM, 06:55 PM, 07:00 PM, 07:05 PM, 07:10 PM, 07:15 PM, 07:20 PM, 07:25 PM, 07:30 PM, 07:35 PM, 07:40 PM, 07:45 PM, 07:50 PM, 07:55 PM]

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now