2014-06-19 3 views
-1

종료 시간이 항상 시작 시간보다 길고 시작 시간과 종료 시간의 차이가 반드시 같아야 Java의 시작 시간과 종료 시간 목록을 생성해야합니다. 최대 5 시간.자바에서 시작 시간과 종료 시간 목록 생성

시간 형식은 24 시간 또는 12 시간 중 하나 일 수 있습니다.

  String myTime = "07:00:00"; 
     SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); 
     Date d = df.parse(myTime); 
     Calendar cal1 = Calendar.getInstance(); 
     cal1.setTime(d); 
     Calendar cal2 = Calendar.getInstance(); 
     cal2.setTime(d); 
     int i=10; 

     List<String> list3 = new ArrayList<String>(); 
     List<String> list4 = new ArrayList<String>(); 


     while(i<50) 
     { 

      cal1.add(Calendar.MINUTE, i); 
      String Start_Time = df.format(cal1.getTime()); 
      cal2.add(Calendar.SECOND, i); 
      String End_Time = df.format(cal2.getTime()); 
      list3.add(Start_Time); 
      list4.add(End_Time); 
      i=i+1; 
     } 
      File file = new File("output9.txt"); 
      FileOutputStream fos = new FileOutputStream(file); 
      PrintStream ps = new PrintStream(fos); 
      System.setOut(ps); 
      for (int j = 0,k=0;j < list3.size()&&k<list4.size(); j++,k++) { 
       String value3 = list3.get(j); 
       String value4 = list4.get(k); 
       System.out.println("\n"); 
       System.out.printf("%s%s%s",value3," ",value4); 
      } 

사람이 그것을 어떻게 좀 도와 주 시겠어요 :

나는하지 모두 시작 시간과 종료 시간에 대한 만족이 코드를하지만, 시작 시간과 종료 시간 기준의 차이를 시도? 사전에

덕분에 ...

답변

1

나는 당신의 문제가 startTimes의 추가 것 같다. 시작 시간에 n 분을 추가하지만이 양을 종료 시간의 초에만 추가하십시오.

그래서 당신이 while 자신에게하는 int 다음과 같이 변경 schould :

cal1.add(Calendar.MINUTE, i); 
String Start_Time = df.format(cal1.getTime()); 
cal2.add(Calendar.MINUTE, i); // add this line 
cal2.add(Calendar.SECOND, i); 
String End_Time = df.format(cal2.getTime()); 

은 당신이 진짜 임의 범위를 원하는 경우를, 당신은 임의의 시간에 시작을 설정하여이를 달성 할 수, 배열에 문자열로 이것을 저장하고 0 시간에서 5 시간 사이의 시간을 추가하고이를 최종 배열에 추가하십시오. 임의의 범위

Random rand = new Random(); 
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); 
Calendar calendar = Calendar.getInstance(); 

List<String> startTimes = new ArrayList<String>(); 
List<String> endTimes = new ArrayList<String>(); 

for (int i = 0; i < 50; i++) { 
    calendar.set(Calendar.HOUR_OF_DAY, 7 + rand.nextInt(7)); // Starting at 07:00:00 
    calendar.set(Calendar.MINUTE, rand.nextInt(60)); 
    calendar.set(Calendar.SECOND, rand.nextInt(60)); 
    String Start_Time = df.format(calendar.getTime()); 

    calendar.add(Calendar.HOUR, rand.nextInt(5)); 
    calendar.add(Calendar.MINUTE, rand.nextInt(60)); 
    calendar.add(Calendar.SECOND, rand.nextInt(60)); 
    String End_Time = df.format(calendar.getTime()); 
    startTimes.add(Start_Time); 
    endTimes.add(End_Time); 
} 

위한

EDIT 예 이것은 7 14 endTimes 5시간 이상 최대 50 startTimes를 생성한다. 예 :

[13:38:56, 08:32:57, 08:50:15, 07:10:49, 13:48:06] 
[13:45:53, 08:36:29, 12:16:35, 11:19:06, 17:19:36] 
관련 문제