Orientation - Handling hours and minutes Java + Postgres

Asked

Viewed 683 times

1

I am finalizing a Time Sheet project for accounting and it is my first project so I have not yet experienced manipulating date and time in java and database, without further delay:

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

Time Check-in Time Check-out Lunch Check-out Lunch Check-out

      08:00 11:00 13:00 17:00

Totaling 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 about turning into long but I could not complete the ideas. I know that by far this is a Minimal, Complete and Verifiable example, the more I like to understand how to do it and then turn around.

After watching several video lessons, I did it the way below, so I can figure out the duration between the hours of input and output, but I really thought it was a scam, who can help me improve this code, turn 23:00 into two integer attributes one of 23 and the other with value 00 does not seem right for me, I thank you already!

 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));
}

1 answer

1


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

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

To extract from the bank, just set the return of the query (resultSet) direct in the object builder DateTime with the getDate. Ex:

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

After getting the schedules, 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 times in the "hand" so you can test but you will take from the bank as shown previously.


Edit: Now that you have put 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));
}
  • 1

    Or simply upgrade to java-8, which already has these native classes.

  • Zulian I did the way you showed me and is giving error in that word Hours said it does not exist, I edited my question and I found a way to do just did not become cool, if you can help me thank.

  • I updated my reply @Dhouglassilvagomes, check it out!

Browser other questions tagged

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