Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8823501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:22:19+00:00 2026-06-14T06:22:19+00:00

Based on the post Calculating difference in days between dates How do I feed

  • 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();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:22:21+00:00Added an answer on June 14, 2026 at 6:22 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this SO post , and this example , I expect that, when
This blog post of December 2008 says that rubygems is broken on Debian-based systems.
I have a search feature on my site that's POST based. Now, however, I
If I submit an iPhone/iPad app that downloads HTML based post bodies, that could
I'm trying to grab Posts and only the comments that belong_to that Post based
I have almost the same question as this . Based on that post I
I looked at previous post based on this but they do not relate. I
I am building an app using listview and lazy adapter based on @fedor post
Again, I was creating buttons dynamically based on this post and now I need
Based on one answer to an earlier post , I'm investigating the possibility of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.