Posted by Darryl Burke on April 9, 2015
JSpinner was designed to work with Dates, Lists or Numbers. With the introduction of a new Date/Time API in Java 8, wouldn’t it be great to be able to create date or time spinners that use the new classes? SpinnerTemporalModel and SpinnerTemporalEditor make that possible.
The classes of primary interest are LocalDate, LocalDateTime, LocalTime, MonthDay, Year and YearMonth, all of which implement the Temporal interface (they also implement TemporalAdjuster and Comparable); hence the choice of names for these two classes. A picture being worth a thousand words, here are some examples of using a JSpinner with some of the available Temporals:
The visual representation of the Temporal is governed by a DateTimeFormatter passed to the constructor of the SpinnerTemporalEditor. This is the code that creates the first of those spinners (the screenshot was captured on 3 April 2015):
SpinnerModel localDateModel = new SpinnerTemporalModel(LocalDate.now(),
LocalDate.of(2013, 1, 1), LocalDate.of(2015, 12, 31),
JSpinner localDateSpinner = new JSpinner(localDateModel);
DateTimeFormatter.ofPattern("d MMMM, uuuu")));
Please note that you will need JDK8 to compile and use these classes.