Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out array of time range in between start time and and time in Java?

Tags:

java

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.

like image 483
mH16 Avatar asked Dec 22 '22 04:12

mH16


2 Answers

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()));
    }
like image 188
MahdeTo Avatar answered Dec 24 '22 02:12

MahdeTo


I modified MahdeTo answer to include start time like this. I am posting whole code below.

enter code here






  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Calendar;
  import java.util.Date;
  import java.util.GregorianCalendar;

 public class TimeRange {
    public static void main(String[] args) {        
    //
    // A string of time information
    //
    String time = "9:00:00";
    String time1 = "21:00:00";

    //
    // Create an instance of SimpleDateFormat with the specified
    // format.
    //
    DateFormat sdf1 = new SimpleDateFormat("hh:mm:ss");
    try {

        Date date = sdf1.parse(time);            
        System.out.println("Date and Time: " + date);


         Date date1 = sdf1.parse(time1);            
        System.out.println("Date and Time1: " + date1);



ArrayList<String> times = new ArrayList<String>();

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);

   if (calendar.getTime().before(date1)){
   times.add(sdf.format(calendar.getTime()));
  System.out.println("Initial time:  "+times);



    while(calendar.getTime().before(date1)) {

   calendar.add(Calendar.MINUTE, 30);
   times.add(sdf.format(calendar.getTime()));
   System.out.println(times);
}

 }


      } catch (Exception e) {
        e.printStackTrace();
      }
   }
}
like image 40
mH16 Avatar answered Dec 24 '22 02:12

mH16