How to stop the date change at 00:00?

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() {

        //for formatting date as desired
        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() {
                //get hour and minute from calendar
                calendar = Calendar.getInstance();
                int hour = calendar.get(Calendar.HOUR);
                int min = calendar.get(Calendar.MINUTE);
                int sec = calendar.get(Calendar.SECOND);

                //check if the field value equals -1
                if (counter == -1) {
                    //and the time at 00:00
                    if (hour == 0 && min == 0 && sec == 2) {
                        //bring the date one day back
                        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;
    }

}

, .

+4
2

, java.util.Date. java.time. - java.sql.Date - , JDBC- JDBC 4.2 , , -.

, . " " 6 6 , , . , LocalDate. LocalDateTime, plusDays( 1 ).

getter, ZoneId ZonedDateTime, , - . A LocalDateTime , . , (DST), , 6 AM 5 AM 7 AM 6:15 AM .

public startAtZone( ZoneId z ) {
    ZonedDateTime zdt = this.startLocalDateTime.atZone( z ) ;
    return zdt ; 
}

Duration. to…, , .

Duration d = Duration.between( ZonedDateTime.now( z ) , myHotelDay.stopAtZone( z ) ) ;

, Half-Open, , - . , , 6:00, , 6 . 6 AM to 6 AM, 05: 59: 59.999 05: 59: 59.999999 05: 59: 59.999999999. >= && < SQL BETWEEN.

, Timer . Executors framework .

, , , yyyy-MM-dd, ISO 8601. java.time / .

+8

, . 00:00, , Audit == false < 6 ..

:

package com.coder.hms.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class CustomDateFactory {

    private LocalDate currentDate;
    private LocalDateTime localDateTime;
    private final ImageIcon icon = new ImageIcon(getClass().
            getResource("/com/coder/hms/icons/dialogPane_question.png"));

    public static void main(String[] args) {

        CustomDateFactory dateFactoryTest = new CustomDateFactory();
        dateFactoryTest.setValidDateUntilAudit(true);
        //for test
        String realDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        System.out.println("REAL DATE : " + realDate);
        System.out.println("PREPARED DATE : " + dateFactoryTest.getDate());


    }

    public CustomDateFactory() {

        localDateTime = LocalDateTime.now();
    }

    private void setValidDateUntilAudit(boolean isAuditted) {

        if (localDateTime.getHour() <= 6 && isAuditted == false) {
            currentDate = LocalDate.now();
            currentDate = currentDate.minusDays(1);

        }

        else if(localDateTime.getHour() < 6 && isAuditted == true) {

            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) {
                currentDate = LocalDate.now();
                isAuditted = false;
            }
            else {
                currentDate = LocalDate.now();
                currentDate = currentDate.minusDays(1);

            }

        } 

        else if (localDateTime.getHour() > 6 && isAuditted == true) {

            int choosedVal = JOptionPane.showOptionDialog(null, "Are you sure about this?",
                    "Approving question", 0, JOptionPane.YES_NO_OPTION, icon, null, null);

            if (choosedVal == JOptionPane.YES_OPTION) {
                currentDate = LocalDate.now();
                isAuditted = false;
            }
            else {
                currentDate = LocalDate.now();
                currentDate = currentDate.minusDays(1);
                isAuditted = false;
            }

        }

    }

    private LocalDate getDate() {
        return currentDate;
    }

}

.

0

Source: https://habr.com/ru/post/1683714/


All Articles