Guidance - Manipulation of hours and minutes Java + Postgres

1

I'm finalizing a worksheet project for an accounting and it's my first project so I still do not experience manipulating date and time in java and database without further ado:

I need only and exclusively to make the following account through the system:

Time Check In Check Out Check Out Check Out Check Out

08:00 11:00 1:00 PM

Totalizing 3 hours worked in the morning and 4 hours worked in the afternoon. I would like to know the best way to manipulate and make this account, I thought of turning into long more I could not finish the ideas. I know that by far this is a Minimum, Complete and Verifiable example, but I like to figure out how to do it and then turn around.

After watching several video lessons, I did the form below, so I can find the duration between the hours of entry and exit, but I really thought it was a gambiarra, who can help me improve this code, turn 23:00 in two integer attributes one of 23 and another one with value 00 does not seem to be right for me, thank you!

 public void teste(){
    int teste1 = (Integer.parseInt(jTextFieldHora1.getText().substring(0,2)));
    int teste2 = (Integer.parseInt(jTextFieldHora1.getText().substring(3,4)));

    int teste3 = (Integer.parseInt(jTextFieldHora2.getText().substring(0,2)));
    int teste4 = (Integer.parseInt(jTextFieldHora2.getText().substring(3,4)));

    LocalTime time1 = LocalTime.of(teste1,teste2);
    LocalTime time2 = LocalTime.of(teste3,teste4);

    System.out.println(ChronoUnit.MINUTES.between(time1, time2));
}
    
asked by anonymous 10.11.2017 / 02:23

1 answer

1

I strongly recommend using the JodaTime library to manipulate dates, times, time zones, etc. in Java, already that the native classes ( Date , Calendar ), although they are better, are neither practical nor intuitive.

Remembering that this library turned native from Java-8, if you are using this version or later, you can directly use the LocalDateTime classes and their methods that are very similar to JodaTime, but the example will use JodaTime. (Thanks @Articuno).

To extract from the database, just set the query return ( resultSet ) directly to the constructor of the object DateTime with getDate . Ex:

DateTime horarioEntrada = new DateTime(resultSet.getDate("horario_entrada"));

Once you get the times, the calculation is quite simple. A testable example:

//criação dos horários
DateTime horaEntrada = new DateTime(2017,11,10, 8,00);
DateTime horaSaidaAlmoco = new DateTime(2017,11,10, 11,00);
DateTime horaRetornoAlmoco = new DateTime(2017,11,10, 13,00);
DateTime horaSaida = new DateTime(2017,11,10, 17,00);

//horas entre os horários
Integer horasManha = Hours.hoursBetween(horaEntrada, horaSaidaAlmoco).getHours();
Integer horasTarde = Hours.hoursBetween(horaRetornoAlmoco, horaSaida).getHours();

//soma das horas
Integer horasTotais = horasManha + horasTarde;

In this example I created the schedules in the "hand" for you to test but you will pick up from the bank as shown above.

Edit: Now that you've put in the code is easier. Ex with LocalTime :

public void teste(){
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HHmm");
    String tempo1 = jTextFieldHora1.getText();
    String tempo2 = jTextFieldHora2.getText();

    LocalTime localTime1 = LocalTime.parse(tempo1, dtf);
    LocalTime localTime2 = LocalTime.parse(tempo2, dtf);

    System.out.println(ChronoUnit.MINUTES.between(localTime1, localTime2));
}
    
10.11.2017 / 13:15