2012-11-03 76 views
0

日期之間天差?回想一下,vS必須大於vE?實際上,我應該分成兩種方法:隨機日期和其他計算差異。計算與載體

/* 
* Randomizacao 
*/ 
package random04DiferencaDataVetor; 

import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Random; 

public class Random04DiferencaDataVetor { 

    public static void main(String[] args) throws ParseException { 


     final long intervalo = 1000000000; 
     Random rnd = new Random(); 
     String[] vE = new String[5]; 
     String[] vS = new String[5]; 
     for (int i = 0; i < vE.length; i++) { 
      /* 
      * arrumar vetores para gerar datas aleatorias 
      * lembrando que vS deve ser maior que vE 
      */ 
      retornaData(); 
     } 
    } 

    static void retornaData() throws ParseException { 
     final long intervalo = 1000000000; 
     Random rnd = new Random(); 
     // formatando as datas 
     DateFormat formato = new SimpleDateFormat("yyyy"); 
     Date anoE = formato.parse("2012"); 
     long timeE = anoE.getTime(); 
     Date anoS = formato.parse("2013"); 
     long timeS = anoS.getTime(); 
     // define o intervalo de datas em 1 ano 
     long tempoIntervalo = timeE - timeS; 
     // randomiza a data de entrada 
     long rndTempoE = timeE + (long) (rnd.nextDouble() * tempoIntervalo); 
     // data entrada 
     String dataE = new SimpleDateFormat("hh:mm dd/MM/yyyy").format(rndTempoE); 
     // randomiza a data de saida 
     long rndTempoS = rndTempoE + (long) (rnd.nextDouble() * intervalo * 2); 
     // data de saida 
     String dataS = new SimpleDateFormat("hh:mm dd/MM/yyyy").format(rndTempoS); 
     // formato de data 
     SimpleDateFormat sdf = new SimpleDateFormat("hh:mm dd/MM/yyyy"); 
     try { 
      Date dataEnt = sdf.parse(dataE); 
      Date dataSaida = sdf.parse(dataS); 
      long differenceMilliSeconds = dataSaida.getTime() - dataEnt.getTime(); 
      long days = differenceMilliSeconds/1000/60/60/24; 
      long hours = (differenceMilliSeconds % (1000 * 60 * 60 * 24))/1000/60/60; 
      long minutes = (differenceMilliSeconds % (1000 * 60 * 60))/1000/60; 
      System.out.println(days + " days, " + hours + " hours, " + minutes + " minutes."); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+1

我無法完全理解你在這裏試圖做什麼...... – cHao

回答

4
  1. 你已經宣佈Array不是Vector。您可以使用如下`矢量聲明:

    Vector<String> vE = new Vector<String>(); 
    Vector<String> vS = new Vector<String>(); 
    

    但你可能要代替Vector如下使用List/ArrayList

    List<String> vE = new ArrayList<String>(); 
    List<String> vS = new ArrayList<String>(); 
    
  2. 添加日期字符串中的載體,可以使用如下add方法:

    String dateString1 = "01/01/2012"; 
    vE.add(dateString1); 
    
  3. 加5個日期你的載體vE,你可以做的爲b elow:

    for (int i = 0; i < 5; i++) { 
        int day = 1+ rnd.nextInt(28); //day from 1 to 28 
        int month = 1+rnd.nextInt(12); //day from 1 to 12 
        int year = 2000 +rnd.nextInt(13); //year from 2000 to 2012 
        String dateString = month+"/"+day+"/"year; 
        vE.add(dateString); 
    } 
    
  4. 您可能想通過矢量vEretornaData();法計算的差異:

     //call in `main` method outside the `for` loop as: 
        retornaData(vE); 
    
        //change method signature as 
        static void retornaData(Vector<String> vE) throws ParseException { 
    
  5. 裏面retornaData(),你可能想獲取兩個日期字符串和計算的區別:

    String date1 = vE.get(0);//use some index 
        String date2 = vE.get(1); //use some index 
    
        //compute the difference between date1 and date2 
    

如果您可以在您的示例程序中使用英語,我可以嘗試建議進一步的更正/改進。

+0

對不起,我不會使用Vector,因爲它是Java中過時的集合。我寧願選擇它的替代品。 +1雖然答案聽起來合法。 – Lion

+0

@Lion:既然你在問題標題中使用了'vector',我以爲你想用'Vector'。更新了使用'List/ArrayList'的答案,這更合理。雖然使用模式沒有太大的區別。 –