0

I want create a list picker that shows only year.

But I want that current year will increase automatically in next year.

I tried do this with DatePicker but it doesn't show only year picker.

How could I do this?

pmb
  • 2,327
  • 3
  • 30
  • 47

2 Answers2

1

Use java.time

Use the java.time classes, the modern replacement for the troublesome old legacy date-time classes.

Determining the year means determining the date. Determining the date requires a time zone. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If not specified, your JVM’s current default time zone is applied. That default varies, even during runtime. So better to always specify your desired/expected time zone explicitly.

ZoneId z = ZoneId.of( "America/Montreal" );

As you want only the year, this time zone issues obviously applies only around the end/beginning of the year. But you might as well program it to generically work all 365 or 366 days of the year.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = LocalDate.now( z );

Lastly interrogate for the year.

int year = ld.getYear();

Calculate next year and last year.

int nextYear = ( year + 1 );
int lastYear = ( year - 1 );

Now use those values to display a number picker.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

This is impossible using the default TimePicker. You could, however, implement MH.'s "hacky" solution.

Alternatively, you could simply use a NumberPicker and make it list the years you would like to have available.

Community
  • 1
  • 1
Shane Duffy
  • 1,117
  • 8
  • 18