add time from other days
This commit is contained in:
@ -4,7 +4,11 @@ import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import de.jottyfan.timetrack.db.done.tables.records.TDoneRecord;
|
||||
@ -41,6 +45,27 @@ public class DoneBean implements Bean, Serializable, Comparable<DoneBean> {
|
||||
this.activity = jobMap.get(r.getFkJob());
|
||||
}
|
||||
|
||||
/**
|
||||
* set the day of timeFrom and timeUntil, keeping the times
|
||||
*
|
||||
* @param day
|
||||
* the day
|
||||
*/
|
||||
public void setDay(Date day) {
|
||||
if (timeFrom != null) {
|
||||
LocalDateTime ldt = timeFrom.toLocalDateTime();
|
||||
LocalDate date = day.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
ldt = ldt.withYear(date.getYear()).withMonth(date.getMonthValue()).withDayOfMonth(date.getDayOfMonth());
|
||||
timeFrom = Timestamp.from(ldt.toInstant(OffsetDateTime.now().getOffset()));
|
||||
}
|
||||
if (timeUntil != null) {
|
||||
LocalDateTime ldt = timeUntil.toLocalDateTime();
|
||||
LocalDate date = day.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
ldt = ldt.withYear(date.getYear()).withMonth(date.getMonthValue()).withDayOfMonth(date.getDayOfMonth());
|
||||
timeUntil = Timestamp.from(ldt.toInstant(OffsetDateTime.now().getOffset()));
|
||||
}
|
||||
}
|
||||
|
||||
public String getTimeSummary() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
if (timeFrom != null) {
|
||||
|
@ -137,6 +137,7 @@ public class DoneModel implements Model, Serializable {
|
||||
|
||||
public boolean insert(JooqFacesContext facesContext) {
|
||||
try {
|
||||
bean.setDay(day);
|
||||
new DoneGateway(facesContext).insert(bean);
|
||||
return true;
|
||||
} catch (DataAccessException | ClassNotFoundException | SQLException e) {
|
||||
|
@ -1,8 +1,13 @@
|
||||
package de.jottyfan.timetrack.moduls.done;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -33,4 +38,30 @@ public class TestDoneBean {
|
||||
assertEquals(ldt.withHour(1).withMinute(0), new DoneBean().getLocalDateTimeFromHHmm("1"));
|
||||
assertEquals(ldt.withHour(5).withMinute(0), new DoneBean().getLocalDateTimeFromHHmm("5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetDay() throws ParseException {
|
||||
String today = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
|
||||
SimpleDateFormat sd = new SimpleDateFormat("dd.MM.yyyy");
|
||||
|
||||
DoneBean bean = new DoneBean();
|
||||
bean.setDay(new Date());
|
||||
assertNull(bean.getTimeFrom());
|
||||
assertNull(bean.getTimeUntil());
|
||||
|
||||
bean.setTimeFromString("08:00");
|
||||
Timestamp ts1 = bean.getTimeFrom();
|
||||
assertEquals(today.concat(" 08:00"), sdf.format(ts1));
|
||||
bean.setDay(sd.parse("01.01.2001"));
|
||||
assertEquals("01.01.2001", sd.format(bean.getTimeFrom()));
|
||||
assertNull(bean.getTimeUntil());
|
||||
|
||||
bean.setTimeUntilString("10:00");
|
||||
Timestamp ts2 = bean.getTimeUntil();
|
||||
assertEquals(today.concat(" 10:00"), sdf.format(ts2));
|
||||
bean.setDay(sd.parse("01.01.2001"));
|
||||
assertEquals("01.01.2001", sd.format(bean.getTimeFrom()));
|
||||
assertEquals("01.01.2001", sd.format(bean.getTimeUntil()));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user