I have table in sql where has to be a total time that each employee worked for a day and because they can leave the office during the worktime I need to do a sum of all the times they were in the office.
this is my code (I simplified it because it is taken out of context):
create table hours_per_day
(
id serial primary key,
day timestamp,
worked_hours timestamp,
id_employee integer references employees(id) ON DELETE SET NULL
);
then I get this:
System.out.println("Date of leave: ");
String dat = br.readLine();
System.out.println("Time of leave: ");
String time = br.readLine();
String tiime = dat + " " + time;
SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date=format.parse(tiime);
mainentry.setCheckOut(new java.sql.Timestamp(date.getTime()));
WorkedHoursPerDay odpr = new WorkedHoursPerDay();
SimpleDateFormat formatt=new SimpleDateFormat("dd/MM/yyyy");
Date dateee=formatt.parse(dat);
odpr.setDay(new java.sql.Timestamp(dateee.getTime()));
long dif = mainentry.getCheckOut().getTime() - z.getCheckIn().getTime();
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(dif),
TimeUnit.MILLISECONDS.toMinutes(dif) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(dif)),
TimeUnit.MILLISECONDS.toSeconds(dif) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(dif)));
String nullls = "00:00:00";
String beg = dat + " " + nullls;
SimpleDateFormat form=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date dattee=form.parse(beg);
odpr.setWorkedHours(new java.sql.Timestamp(dattee.getTime()));
odpr.insert();
odpr.update();
this is my code:
public void update() throws SQLException {
if (idEmployee== null) {
throw new IllegalStateException("id employee does not exist");
}
try (PreparedStatement s = DbContext.getConnection().prepareStatement("UPDATE hours_per_day SET worked_hours = worked_hours + ? WHERE id_employee = ? AND day = ?")) {
s.setTimestamp(1, howManyHoursWorked)
s.setInt(2, idEmployee);
s.setTimestamp(3, day);
s.executeUpdate();
}
}
but this is the result:
ERROR: invalid input syntax for type interval: "2019-01-01 04:00:00+01"
I don't know how to add those worked hours to already existing hours in database. Maybe I should change the String time1 into interval but I dont know how to do that.