battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.


battle programming a forum for elite programmers with extreme will power to sharpen their skills
 
HomeGallerySearchRegisterLog in
 

 an update to the boolean time gate class I have made

Go down 
AuthorMessage
Moti Barski
super
super
Moti Barski

Posts : 562
Join date : 2011-08-02

an update to the boolean time gate class I have made Empty
PostSubject: an update to the boolean time gate class I have made   an update to the boolean time gate class I have made EmptyThu Mar 25, 2021 5:40 am

Code:
import java.util.Calendar;
import java.util.Date;

public class ZeroTimeGate {
    // a gate that only opens x minutes after it has been set
    private int pause = 1;
    private Date openedGate = new Date();
    private Date checkPoint = new Date();

    public ZeroTimeGate(int minutes) {
        super();
        this.pause = minutes;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
        }
    }

    public ZeroTimeGate() {
    }

    public Boolean isClosed() {
        return openedGate.before(new Date());
    }

    public Boolean isOpen() {
        return !openedGate.before(new Date());
    }
    public void open() {
        this.openedGate = addMinutesToJavaUtilDate(new Date(), pause);
    }

    public void open(int minutes) {
        Date now = new Date();
        openedGate = addMinutesToJavaUtilDate(now, minutes);
    }

    private Date addMinutesToJavaUtilDate(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutes);
        return calendar.getTime();
    }

    public void setPause(int pause) {
        if (pause < 60 && pause > 0) {
            this.pause = pause;
        }
    }

    public void resetCheckPoint() {
        this.checkPoint = new Date();
    }

    public int givenTwoDateTimesInJava8_whenDifferentiatingInSeconds_thenWeGetTen() {
        Date now = new Date();
        long diff = now.getTime() - this.checkPoint.getTime();
        long diffSeconds = diff / 1000 % 60;
        // long diffMinutes = diff / (60 * 1000) % 60;
        // long diffHours = diff / (60 * 60 * 1000) % 24;
        // long diffDays = diff / (24 * 60 * 60 * 1000);
        // System.out.print(diffDays + " days, ");
        // System.out.print(diffHours + " hours, ");
        // System.out.print(diffMinutes + " minutes, ");
        // System.out.print(diffSeconds + " seconds.");
        return (int) diffSeconds;
    }
}

changed Thread.sleep(1000); to Thread.sleep(100);
after experimenting this seems to work better

_________________
MB over and out emp it up
Back to top Go down
 
an update to the boolean time gate class I have made
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: the LivinGrimoire-
Jump to: