1

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?

4 Answers4

5

tl;dr

LocalDate.now( ZoneId.systemDefault() )
         .isBefore( LocalDate.of( 2016 , Month.DECEMBER , 25 ) )

Using java.time

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 
}

Dynamic update

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.


About java.time

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?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks, I feel like you made it so easy for me but I'm still not getting it. = ( – Barron Donovan Dec 24 '16 at 20:07
  • The symbols aren't being recognized do I have to declare them elsewhere? I imported calendar and everything at the beginning – Barron Donovan Dec 24 '16 at 20:08
  • @BarronDonovan (a) Do not use `java.util.Calendar` – That class is a troublesome mess! Use only the java.time classes; these entirely supplant the legacy classes. (b) **Read my *entire* answer** with special attention to second sentence and to 3rd outer bullet. – Basil Bourque Dec 24 '16 at 20:11
1

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.

Community
  • 1
  • 1
ziLk
  • 3,120
  • 21
  • 45
0

You can make use of a TimerTask and a Timer.

  • The button is disabled by default, so it has to be enabled at a specific time.
  • The only task of the TimerTask is to enable the button
  • The Timer is used to fire the created TimerTask at a specific point in time.
Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
0

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
         }
     });
}