I am working on a site management application [desktop], and I noticed that the time change system is not 00:00, it is between 1AM and 6AM so reservations and room status, etc. must remain until the audit time. When the user does an audit, a new day will begin.
This is why I need to create a method that stops the date change at midnight and returns a new date when I click the audit button. In short, I have to create a central system for the date.
As a result; when I use this date in all classes, each method will work synchronously [lock, reserve, register, check, etc.], but I could not find a good way to do this.
I am reflecting on the following code:
package com.coder.hms.utils;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class CustomDateFactory {
private Date date;
private Calendar calendar;
private SimpleDateFormat sdf;
public CustomDateFactory() {
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
public void setValidDateUntilAudit(int counter) {
final Timer timer = new Timer();
final TimerTask task = new TimerTask() {
@Override
public void run() {
calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR);
int min = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);
if (counter == -1) {
if (hour == 0 && min == 0 && sec == 2) {
calendar.add(Calendar.DATE, counter);
date = calendar.getTime();
}
} else {
date = new Date();
}
}
};
timer.schedule(task, 0, 100);
}
public Date getDate() {
final String today = sdf.format(date);
final LocalDate ld = LocalDate.parse(today);
date = java.sql.Date.valueOf(ld);
return date;
}
}
:
public class DateFactoryTest {
private LocalDate currentDate;
private LocalDateTime localDateTime;
public DateFactoryTest() {
currentDate = LocalDate.now();
localDateTime = LocalDateTime.now();
}
private void setTheDate(boolean isAuditted) {
if (localDateTime.getHour() <= 6 && isAuditted == false) {
currentDate.minusDays(1);
} else if (localDateTime.getHour() > 6 && isAuditted == true) {
ImageIcon icon = new ImageIcon(getClass().getResource("/com/coder/hms/icons/dialogPane_question.png"));
int choosedVal = JOptionPane.showOptionDialog(null, "You're doing early audit, are you sure about this?",
"Approving question", 0, JOptionPane.YES_NO_OPTION, icon, null, null);
if (choosedVal == JOptionPane.YES_OPTION) {
isAuditted = false;
}
}
}
private LocalDate getDate() {
return currentDate;
}
}
, .