After keeping getting "warnings" of my question is not good enough I will try again and get every single thing added to this question.
I'm trying to make an application where I can calculate my salary (hourly paid). For this there is certain requirements since different time slots/days have different appendices.
My most important requirement, and the one I'm strugling with is to check if my workday has been between the timeslot of 01:00-06:00, if so how many hours. E.g. start Monday 23:00 end Tuesday 05:00 I have worked 06:00 hours and 04:00 hours has been between 01:00-06:00
This is my current code where I have tried with different types of if-statements to get the interval, but I can't get it to work in all situations (no matter what I type), this is because of the difficulty with it possibly is going from one day to another.
(My notes etc. are in danish, but this question is only regarding table 2,3 and 8 and is all in my tableChanged method)
import java.awt.*;
import java.awt.event.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class TimeDifferenceCalculator222 extends JFrame {
private JTextField dateField;
private JTextField dateField1;
private JTable table;
private DefaultTableModel model;
public TimeDifferenceCalculator222() {
super("Time Difference Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Opret tekstfelt for dato
dateField = new JTextField(10);
dateField.setToolTipText("Indtast dato i formatet dd/MM/yyyy");
// Opret tabel for datoer og start/slut-tidspunkter
model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return column != 0;
}
};
table = new JTable(model);
model.addColumn("Dato");
model.addColumn("Ugedag");
model.addColumn("Starttidspunkt");
model.addColumn("Sluttidspunkt");
model.addColumn("Differencetid");
model.addColumn("Branchetillæg");
model.addColumn("VC1 tillæg");
model.addColumn("Nattetillæg");
model.addColumn("Nattetimer (01:00-06:00)");
model.addColumn("Weekendtillæg (lørdag)");
model.addColumn("Weekend / Helligdage");
// Tilføj 31 rækker til tabellen
LocalDate startDate = LocalDate.now();
for (int i = 0; i < 31; i++) {
model.addRow(new Object[] { startDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")), "", "", "" });
startDate = startDate.plusDays(1);
}
// Tilføj lytter til dato-feltet
dateField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
LocalDate date = LocalDate.parse(dateField.getText(), DateTimeFormatter.ofPattern("dd/MM/yyyy"));
LocalDate endDate = date.plusDays(30);
int row = 0;
for (LocalDate d = date; !d.isAfter(endDate); d = d.plusDays(1)) {
model.setValueAt(d.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")), row, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(d.getYear(), d.getMonthValue() - 1, d.getDayOfMonth());
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
String dayOfWeekString;
switch (dayOfWeek) {
case Calendar.SUNDAY:
dayOfWeekString = "Søndag";
break;
case Calendar.MONDAY:
dayOfWeekString = "Mandag";
break;
case Calendar.TUESDAY:
dayOfWeekString = "Tirsdag";
break;
case Calendar.WEDNESDAY:
dayOfWeekString = "Onsdag";
break;
case Calendar.THURSDAY:
dayOfWeekString = "Torsdag";
break;
case Calendar.FRIDAY:
dayOfWeekString = "Fredag";
break;
case Calendar.SATURDAY:
dayOfWeekString = "Lørdag";
break;
default:
dayOfWeekString = "";
break;
}
model.setValueAt(dayOfWeekString, row, 1);
row++;
}
} catch (DateTimeParseException ex) {
JOptionPane.showMessageDialog(TimeDifferenceCalculator222.this, "Ugyldig datoformat", "Fejl", JOptionPane.ERROR_MESSAGE);
}
}
});
// Tilføj lytter til tabelen
table.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE && e.getColumn() >= 2 && e.getColumn() <= 3) {
int row = e.getFirstRow();
String startStr = (String) model.getValueAt(row, 2);
String endStr = (String) model.getValueAt(row, 3);
if (!startStr.isEmpty() && !endStr.isEmpty()) {
LocalTime start = LocalTime.parse(startStr, DateTimeFormatter.ofPattern("HH:mm"));
LocalTime end = LocalTime.parse(endStr, DateTimeFormatter.ofPattern("HH:mm"));
LocalDateTime startDateTime = LocalDateTime.of(LocalDate.parse((String) model.getValueAt(row, 0), DateTimeFormatter.ofPattern("dd/MM/yyyy")), start);
LocalDateTime endDateTime = LocalDateTime.of(LocalDate.parse((String) model.getValueAt(row, 0), DateTimeFormatter.ofPattern("dd/MM/yyyy")), end);
if (end.isBefore(start)) {
endDateTime = endDateTime.plusDays(1);
}
Duration duration = Duration.between(startDateTime, endDateTime);
long minutes = duration.toMinutes();
long hours = minutes / 60;
long remainingMinutes = minutes % 60;
String differenceTime = String.format("%02d:%02d", hours, remainingMinutes);
model.setValueAt(differenceTime, row, 4);
// Beregn antal timer mellem 01:00 og 06:00 og sæt i kolonne 8
LocalTime oneAM = LocalTime.of(1, 0);
LocalTime sixAM = LocalTime.of(6, 0);
if (start.isBefore(oneAM) && end.isAfter(oneAM) || start.isAfter(oneAM) && end.isBefore(sixAM) || start.isAfter(oneAM) && end.isAfter(sixAM)) {
LocalDateTime start1am = LocalDateTime.of(LocalDate.parse((String) model.getValueAt(row, 0), DateTimeFormatter.ofPattern("dd/MM/yyyy")), oneAM);
LocalDateTime end6am = LocalDateTime.of(LocalDate.parse((String) model.getValueAt(row, 0), DateTimeFormatter.ofPattern("dd/MM/yyyy")), sixAM);
if (start.isBefore(oneAM) && end.isBefore(sixAM)) {
start1am = LocalDateTime.from(oneAM);
}
if (start.isAfter(oneAM) && end.isBefore(sixAM)) {
start1am = LocalDateTime.from(startDateTime);
end6am = LocalDateTime.from(endDateTime);
}
if (end.isBefore(sixAM)) {
end6am = endDateTime;
}
Duration durationBetween1and6 = Duration.between(start1am, end6am);
long minutesBetween1and6 = durationBetween1and6.toMinutes();
long hoursBetween1and6 = minutesBetween1and6 / 60;
long remainingMinutesBetween1and6 = minutesBetween1and6 % 60;
String hoursMinutesBetween1and6 = String.format("%02d:%02d", hoursBetween1and6, remainingMinutesBetween1and6);
model.setValueAt(hoursMinutesBetween1and6, row, 8);
}
}
}
}
});
// Opret scroll-pane og tilføj tabelen til den
JScrollPane scrollPane = new JScrollPane(table);
// Tilføj komponenter til frame
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(dateField, BorderLayout.NORTH);
c.add(scrollPane, BorderLayout.CENTER);
// Vis frame
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new TimeDifferenceCalculator222();
}
}