sorry if this is a noob question.
I made a quick android app to give away gift cards for christmas but I'd like to limit people to "unwrapping" their present after Christmas, is there any way I can disable the button to do so until midnight?
sorry if this is a noob question.
I made a quick android app to give away gift cards for christmas but I'd like to limit people to "unwrapping" their present after Christmas, is there any way I can disable the button to do so until midnight?
LocalDate.now( ZoneId.systemDefault() )
.isBefore( LocalDate.of( 2016 , Month.DECEMBER , 25 ) )
The modern way to do date-time handling is with the java.time classes. Much of that functionality is back-ported to Android (see section below).
To determine the current date, a time zone is required. For any given moment, the date varies around the globe by zone. Christmas starts first in the Pacific in zones such as Pacific/Auckland
, later in Asia/Kolkata
, still later in Europe/Paris
, and even later in America/Montreal
. This explains why Santa flies his reindeer from the East to the West.
When important, ask the user for their intended time zone. Otherwise, use the JVM’s current default zone.
ZoneId z = ZoneId.systemDefault() ;
LocalDate today = LocalDate.now( z );
Determine Christmas.
LocalDate xmas = LocalDate.of( 2016 , Month.DECEMBER , 25 ) ;
Compare.
if( today.isBefore( xmas ) ) {
… disable button
} else {
… enable button
}
To dynamically update the button to enable as the clock ticks over to Christmas, run a background thread. Every so often, check for the current date and compare to the deadline date. A ScheduledExecutorService
is the modern way to run a repeated background task like this. Note that you can set an initial delay to get you close to the beginning of Xmas, if desired, to decrease the number of superfluous checks. Search Stack Overflow for many Questions and Answers on this topic. Tip: Be sure to put a try-catch at the highest level of your code running in your executor service as any uncaught Exception
causes the service to silently cease.
Be careful to access the button in the GUI from your background thread in a thread-safe manner. Again, search Stack Overflow for many Questions and Answers on this topic.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
There are two option to get current time to do that.
1- Getting current time from User device.
This is a bad solution because user can change time easily on Device settings panel.
2- Getting current time from internet.
This is a much better solution. But your application will be need a internet connection.
You can make use of a TimerTask and a Timer.
you can Look over it:
first of all Create Two Date Objects and set one object as Christmas (static) and another as today (dynamic) as shown below :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buttonXMas=findViewById(R.id.button1);
buttonXMas.setEnabled(false);
SimpleDateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");
Date xmas= dateFormat.parse("25/12/2016");
Date today = new Date();
if(xmas.after(today)){
buttonXMas.setEnabled(true);
}
buttonXMas.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}