java - How to stop date changing at 00:00 o'clock? -
i'm working on hotel management application [desktop] , noticed, date changing time hotels system not 00:00 , it's between 1am , 6am reservations , room status etc. should stay until audit time.when user make audit new day start.
that's why need create method stop date change @ midnight , return new date when audit button clicked. briefly have create central system date.
as result; when use date in classes every methods work synchronously[blockade, reservations, check in, check out etc.] couldn't find way this.
i'm thinking around 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 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 , minute calendar calendar = calendar.getinstance(); int hour = calendar.get(calendar.hour); int min = calendar.get(calendar.minute); int sec = calendar.get(calendar.second); //check if field value equals -1 if (counter == -1) { //and time @ 00:00 if (hour == 0 && min == 0 && sec == 2) { //bring date 1 day 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; } }
after comments , helps changed code :
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 audit, sure this?", "approving question", 0, joptionpane.yes_no_option, icon, null, null); if (choosedval == joptionpane.yes_option) { isauditted = false; } } } private localdate getdate() { return currentdate; } }
all answers, different ideas acceptable.
avoid troublesome old date-time classes such java.util.date
. supplanted java.time classes. among obsolete classes java.sql.date
– may have use if jdbc driver not yet updated jdbc 4.2 , later, if so, minimize use , not use these objects in business logic.
as other people commented, do not hack meaning of date. if “hotel day” runs 6 6 am, create class represent that. define member official date, of type localdate
. define members start , stop, of type localdatetime
, stop plusdays( 1 )
.
define pair of getter methods each taking zoneid
argument, , returning zoneddatetime
, each true exact moment when hotel-day starts , stops. localdatetime
not actual moment, , has no meaning until specify time zone. anomalies such daylight saving time (dst) mean 6 start or stop time might 5 or 7 or 6:15 on particular date.
public startatzone( zoneid z ) { zoneddatetime zdt = this.startlocaldatetime.atzone( z ) ; return zdt ; }
calculate span of time duration
. call to…
methods total number of seconds, milliseconds, or nanoseconds.
duration d = duration.between( zoneddatetime.now( z ) , myhotelday.stopatzone( z ) ) ;
note best approach defining spans of time half-open approach beginning inclusive , ending exclusive. means hotel-day starting @ 6 runs to, not include, following 6 am. that means 6 6 am
, no bothering determine 05:59:59.999 or 05:59:59.999999 or 05:59:59.999999999. search >= && <
logic, , not use sql between
.
by way, timer
legacy now. read executors framework, , search stack overflow.
by way, format used, yyyy-mm-dd
, defined iso 8601 standard. java.time classes use standard formats default when parsing/generating strings.
Comments
Post a Comment