2011-12-24 21 views
3

私は2つの時間の間に時間範囲を作成しようとしています。私は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は頭字語ではありません。すべて大文字で設定しないでください。 –

+0

ありがとう..私は編集しました。 – mH16

答えて

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:00、10:30、11:00などが必要です私のPHPスクリプトは次のようになります:[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 – mH16

+0

時間の部分だけを表示して今すぐ試してください – MahdeTo

+0

あなたはこれを見てくださいhttp://stackoverflow.com/questions/8623058/how-to-set-start-time-and-end-time-with-an-interval -of-30-minutes-in-timepicker – mH16

2

おそらくJoda Timeを使用してこれを実行している可能性があります。 Durationクラスがあり、.plus()メソッドを使用してDateTimeオブジェクトに追加できます。

実際の作業と同じように、基本日時を取得し、継続時間を延長し、サイクルを繰り返し、配列に配列を追加します(実際には、すべてのオブジェクトが一意であるため、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

後でアンドロイドアプリにこのロジックが必要です。私はJODA時間がアンドロイドのために働くかどうかを知っています。 – mH16

+0

です。私はAndroidアプリケーションでも使用しています。 – Yogu

+0

@ Yogu:これを行うためのサンプルコードを提供できますか? – mH16

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(); 
     } 
    } 
} 
関連する問題