While iteration with date and time

Asked

Viewed 90 times

0

How to organize the method JAVA below, to get the output as in the example. I have an array of times (would be input and output), and I need to associate these times to a date range, while I was with input and output on the same day I managed to do, but in the model below, the input occurs in one day and the output in the other, and one day before and one after the day off there’s only one way in or one way out...

output example I need:

    1/1/2018 23:00
    2/1/2018 23:00
    2/1/2018 06:00
    3/1/2018 23:00
    3/1/2018 06:00
    4/1/2018 06:00
    5/1/2018 00:00 (folga)
    5/1/2018 00:00 (folga)
    6/1/2018 23:00
    7/1/2018 23:00
    7/1/2018 06:00
    8/1/2018 23:00
    8/1/2018 06:00
    9/1/2018 06:00
    10/1/2018 00:00 (folga)

Method:

 public void teste5(){

        String []hora = new String[8];
        String []result = new String[10];
        int dia = 1;
        int i=0;



        hora[0]="23:00";
        hora[1]="06:00";
        hora[2]="23:00";
        hora[3]="06:00";
        hora[4]="23:00";
        hora[5]="06:00";
        hora[6]="00:00";
        hora[7]="00:00";         

        while(dia < 10){ //aqui será definido um intervalo de datas


            result[i] = dia+"/1/2018"+hora[i];
            System.out.println(result[i]);

           dia++;
        }

    }

After I get this method to work I will store in MySQL as below:

  entrada              saida
1/1/2018 23:00     2/1/2018 06:00
2/1/2018 23:00     3/1/2018 06:00
3/1/2018 23:00     4/1/2018 06:00
5/1/2018 00:00     5/1/2018 00:00 
6/1/2018 23:00     7/1/2018 06:00
7/1/2018 23:00     8/1/2018 06:00
8/1/2018 23:00     9/1/2018 06:00
10/1/2018 00:00    10/1/2018 00:00
11/1/2018 23:00    12/1/2018 06:00
  • Where is the date array? I only see a string array with hours. The code does not match the doubt.

  • The date I can associate later, in place of the "day" of the example.

  • Your problem involves the date, without it, your code makes no sense at all with your doubt.

  • But the same as can be done with the "day" of the example, can be done with the date.. add a day for example, and merge with the time. I have only one array with the schedules, which is fixed, the date will be set within the 'While''.

  • It is not the same thing no, whole is whole, date is date, are totally different ways of organizing, and as.

  • But the date is not fixed, I do not know how to mount it within the while, the date could be any period, between 01/01/2018 to 10/01/2018 for example or any other. Within this period I have to distribute the schedule array repeatedly, with input at 23:00 and output at 06:00 the other day.

  • 1

    Dude, make a better example of your doubt. Currently not giving to infer a lot about the hours "23" and "06", only that can be off from one day to the next.. Have some standard interval to set between days to specify a slack or something like that... Explain better.. And it is better to deal with the specific types for Dates, Hours and Intervals; Using integers and strings for this ends up complicating, not to mention that it is gambiarra and addition of complexity..

  • 1

    I agree with @Marlysson. Date and time are temporal representations, not to treat this unless as "temporal representations". What you’re trying to do sounds like you really have. Maybe by doing it the right way, treating both as temporal, it’ll even make it easier to help you and help you deal with the problem.

  • Java already has numerous Apis to handle both date and time only, as well as to handle both at the same time, so reinvent the wheel?

  • @Marlysson - Yes there is a fixed interval for slack - example 3x1, 3 days worked and one off, always this way the iteration, always between 23:00 of a day until 06:00 of the other day, but really, I think it will be easier and more correct to use some native API to do this instead of adding dates inside while, I will try to redo the method here .

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.