2017-08-28 17 views
-2

私は24時間の日を48タイムスロットに分割しました。だから私はこのようにそれをハードコード: (Javaコード)48時間で24時間ダイビング

private static void createMap() { 
    timeSlotMap.put(0, "0:30"); 

    timeSlotMap.put(1, "01:00"); 
    timeSlotMap.put(2, "01:30"); 

    timeSlotMap.put(3, "02:00"); 
    timeSlotMap.put(4, "02:30"); 
    ... 
    ... 
    timeSlotMap.put(45, "23:00"); 
    timeSlotMap.put(46, "23:30"); 

    timeSlotMap.put(47, "24:00"); 
} 

これを行うには良い方法はありますか?

ありがとうございました

+0

何か[time unit](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) – litelite

+0

ええ、でも夏時間になるとどうなりますか。 .. – blackghost

+0

どのような種類のタイムスロットが24:00ですか? – shmosel

答えて

3

LocalTimeを使用して別の方法:いくつかの種類とループのような

LocalTime time = LocalTime.MIDNIGHT; 
int minutesToAdd = 30; 
Map<Integer, String> times = new TreeMap<>(); 

for(int i = 1; i < 48; i++){ 
    times.put(i - 1,time.plusMinutes(minutesToAdd * i).toString()); 
} 

times.put(47, "24:00"); 
1

確かに、これがループです。 String.format機能で、例えば、最初の引数にループインデックスを作成し、最初から二番目の構築:

 private static void createMap() { 
      for (int i = 0; i < 48; i++) { 
       int t = (i + 1) * 30; 
       timeSlotMap.put(i, String.format("%02d:%02d", t/60, t % 60)); 
      } 
     } 
+0

ありがとうございました! – user1860447