2011-12-24 255 views
3

我想在兩次之間創建一個時間範圍。我可以在PHP 這個代碼給我的時間間隔30分鐘,當我提供開始時間,結束時間和時間間隔。下面是php腳本。如何在Java中的開始時間和時間之間找出時間範圍數組?

//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 
) 

    */ 

    ?> 

我需要在JAVA代碼中做同樣的事情。我認爲這對其他人會有幫助。

+0

Java不是縮寫,不應該設置爲全部大寫。 – 2011-12-24 11:50:22

+0

謝謝..現在我編輯了。 – mH16 2011-12-24 11:52:52

回答

4

辦法做到這一點使用唯一的Java API是使用Calendar類

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

其工作,但它給出完整的日期和時間。我需要像10點,10點30分,11點等可以你給我的確切格式返回時間,正如我上面提到的。我的PHP腳本給出:[0] => 9:30:00 [1] => 10:00:00 [2] => 10:30:00 => 11:00:00 [4] => 11:30:00 [5] => 12:00:00 [6] => 12:30:00 – mH16 2011-12-24 17:24:09

+0

好的,我將其更改爲只顯示時間部分,現在嘗試 – MahdeTo 2011-12-24 19:00:51

+0

你可以看看這個http://stackoverflow.com/questions/8623058/how-to-set-start-time-and-end-time-with-an-interval -30分鐘在時間選擇器 – mH16 2011-12-25 08:10:54

2

你最好打賭可能是用Joda Time這樣做。它有一個Duration類,您可以使用.plus()方法將其添加到DateTime對象。就像你一樣(事實上,下面的代碼使用了Set,因爲所有的對象都是唯一的)。將日期時間設置爲一個數組,並將日期時間添加到數組中,就像你一樣(實際上,下面的代碼使用Set,因爲所有對象都是唯一的)。

示例代碼:

public final class JodaTest 
{ 
    private static final DateTimeFormatter FORMAT 
     = DateTimeFormat.forPattern("HH:mm"); 

    public static void main(final String... args) 
    { 
     final DateTime start = FORMAT.parseDateTime("09:30"); 
     final DateTime end = FORMAT.parseDateTime("17:30"); 

     final Duration duration = Minutes.minutes(30).toStandardDuration(); 

     final Set<DateTime> set = new LinkedHashSet<DateTime>(); 

     DateTime d = new DateTime(start); 

     do { 
      set.add(d); 
      d = d.plus(duration); 
     } while (d.compareTo(end) <= 0); 

     for (final DateTime dt: set) 
      System.out.println(FORMAT.print(dt)); 

     System.exit(0); 
    } 
} 
+1

我以後需要這個邏輯的Android應用程序。我知道JODA時間是否適用於android。 – mH16 2011-12-24 11:43:32

+0

它的確如此。我也在android應用程序中使用它。 – Yogu 2011-12-24 11:50:05

+0

@Yogu:你能否爲我提供示例代碼來做到這一點。 – mH16 2011-12-24 11:51:45

3

我修改MahdeTo的答案,包括開始時間是這樣的。我在下面發佈整個代碼。

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(); 
     } 
    } 
} 
相關問題