Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:54:05+00:00 2026-05-27T15:54:05+00:00

I am trying to create a time range in between two times. I am

  • 0

I am trying to create a time range in between two times. I am able to do it in PHP
This code gives the array of times with 30 minutes interval when i supply start time , end time and interval. Below is the php script.

//timerange.php


   <?php 

/** 
* create_time_range  
*  
* @param mixed $start start time, e.g., 9:30am or 9:30 
* @param mixed $end   end time, e.g., 5:30pm or 17:30 
* @param string $by   1 hour, 1 mins, 1 secs, etc. 
* @access public 
* @return void 
*/ 
function create_time_range($start, $end, $by='30 mins') { 

   $start_time = strtotime($start); 
   $end_time   = strtotime($end); 

   $current    = time(); 
   $add_time   = strtotime('+'.$by, $current); 
   $diff       = $add_time-$current; 

   $times = array(); 
   while ($start_time < $end_time) { 
       $times[] = $start_time; 
       $start_time += $diff; 
    } 
  $times[] = $start_time; 
  return $times; 
 } 

  // create array of time ranges 
  $times = create_time_range('9:30', '17:30', '30 mins'); 

   // more examples 
  // $times = create_time_range('9:30am', '5:30pm', '30 mins'); 
   // $times = create_time_range('9:30am', '5:30pm', '1 mins'); 
 // $times = create_time_range('9:30am', '5:30pm', '30 secs'); 
 // and so on 

// format the unix timestamps 
   foreach ($times as $key => $time) { 
     $times[$key] = date('g:i:s', $time); 
  } 


      print '<pre>'. print_r($times, true).'</pre>'; 
    /* 
   * result 
   * 
    Array 
  ( 
  [0] => 9:30:00 
  [1] => 10:00:00 
  [2] => 10:30:00 
  [3] => 11:00:00 
  [4] => 11:30:00 
  [5] => 12:00:00 
  [6] => 12:30:00 
  [7] => 1:00:00 
  [8] => 1:30:00 
  [9] => 2:00:00 
  [10] => 2:30:00 
  [11] => 3:00:00 
  [12] => 3:30:00 
  [13] => 4:00:00 
   [14] => 4:30:00 
   [15] => 5:00:00 
   [16] => 5:30:00 
 ) 

  */ 

     ?>

I need to do the equivalent in JAVA code. I think this would be helpful to others.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:54:05+00:00Added an answer on May 27, 2026 at 3:54 pm

    A way to do this using only java APIs is to use the Calendar class

        Date startTime = ...//start
        Date endTime = ../end
        ArrayList<String> times = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTime(startTime);
        while(calendar.getTime().before(endTime)) {
             calendar.add(Calendar.MINUTE, 30);
             times.add(sdf.format(calendar.getTime()));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a one time download link in php as my
I'm trying to create a new div each time a text field contained in
I'm trying to create an instance of a class at run time. The classes
I'm trying to dynamically create a connection string during compile time: private static string
I'm trying to use the Timespan class to create a start time and a
I'm trying, for the first time, to create a GUI in Ruby using Tk.
I am trying to create an array for years which i will use in
I am trying to create a simple Applescript that is activated each time I
I'm trying to copy and paste a range, to create a 28 by 28
I am trying to create tiles using generate_tiles.py and every time I run the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.