0

Based on the post Calculating difference in days between dates

How do I feed the vectors vE and vS with random dates and then return the difference between your dates? Recalling that vS must be greater than vE? Actually, I should separate into two methods: a randomized dates and other calculates the difference.

/*
 * 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();
        }
    }
}
Community
  • 1
  • 1
Regis Santos
  • 3,469
  • 8
  • 43
  • 65

1 Answers1

4
  1. You have declared Array not the Vector. You may use `Vector declaration as below:

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

    But you may want to use List/ArrayList in place of Vector as below:

     List<String> vE = new ArrayList<String>();
     List<String> vS = new ArrayList<String>();
    
  2. To add the date strings in the vectors, you can use add method as below:

     String dateString1 = "01/01/2012";
     vE.add(dateString1);
    
  3. To add 5 dates your Vector vE, you may do as below:

     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. You may want to pass Vector vE in your retornaData(); method to compute the differences:

         //call in `main` method  outside the `for` loop as:
         retornaData(vE);
    
         //change method signature as 
         static void retornaData(Vector<String> vE) throws ParseException {
    
  5. Inside retornaData(), you may want to retrieve two date string and compute the difference:

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

If you could use English in your sample program, I may try advising further corrections/improvements.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • Sorry but I would not use `Vector` as it is an obsolete collection in Java. I would prefer its alternatives instead. +1 though as the answer sounds legitimate. – Lion Nov 03 '12 at 04:05
  • @Lion: Since you had used `vector` in the question title, I thought you wanted to use `Vector`. Updated the answer to use `List/ArrayList`, which is more legitimate. There is not much difference in usage pattern though. – Yogendra Singh Nov 03 '12 at 04:12