Sad to say, there is no such control in Java FX 2. However, Java FX 8 has one. Then again, if you want to maintain your rule that you must never force the user to update to the latest version just to be able to see a date picker, the only option is to make one.
Luckily, somebody else did the dirty work for us. Marko Jacob extended a more flexible version from Christian Schudt and it works flawlessly. Plus, using it is fairly easy too.
The downside to this is that you cannot import this control in the Java FX Scene Builder. You will have to add the DatePicker control programmatically.
Download his JavaFX Date Picker Release on GitHub.
So here’s the thing. I wanted my widget to run a task every 12 midnight. The attribute of Intent.ACTION_DATE_CHANGED does not work.
I had read posts in forums that say if you switch back and forth setting the date and time for experimentation to see if the task will run, nothing happens. So the only workaround left is to use an AlarmManager.
Yup, you have to use it rather than being forced to use Intent.ACTION_TIME_TICK which is battery draining as this event will occur every minute.
To set up your AlarmManager to send a broadcast every 12 midnight, do this:
Take note that you have to use getBroadcast() in the PendingIntent class if you want the task in the BroadcastReceiver to be executed. If it is code in a service that you want to run, then call getService().
Just now I encountered another weird behavior in Android. Using the Calendar class does not yield the desired results. So I have a custom date as a String, converted it to a java.util.Date object then passed it to the Calendar object’s setTime() method.
When I called the Calendar object’s get() method using Calendar.YEAR as the parameter, it returned the current year instead of the year that was set to it.
I have no idea why it acts like this but since this behavior persists in the Android emulator, the same should happen in a real smartphone that runs Android.
Better avoid using the Calendar class if it does not give you the results that you want.