简体   繁体   中英

Android - How do you retrieve Week of Year from a onDateChangedListener?

I'm in a bit of a pickle. I'm building an application where I display the Day, Month, Year and Week Number from a DatePicker. When a date is selected, the date and the week number should then be displayed on screen. I've managed to display the correct date, but I struggle to display the Week Number

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);

    datePicker = (DatePicker) findViewById(R.id.kalender);
    datoText = (TextView) findViewById(R.id.kalenderText);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    final int week = calendar.get(Calendar.WEEK_OF_YEAR);

    datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            String dato = dayOfMonth + " / " + (monthOfYear + 1) + " / " + year;
            datoText.setText(dato);
        }
    });

So, my question is this: How can I display the week number of the selected date?

Even if you didn't want to display the week number, you should use a formatter for formatting the date for display. That in turn will take nicely care of the week number too. I suggest you declare the formatter a constant in your class:

private static final DateTimeFormatter DATE_FORMATTER
        = DateTimeFormatter.ofPattern("d/M/u 'Uke' w", Locale.forLanguageTag("no"));

Now the rest is pretty straightforward:

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        LocalDate dato = LocalDate.of(year, monthOfYear + 1, dayOfMonth);
        datoText.setText(dato.format(DATE_FORMATTER));
    }

This will display a string like 2/4/2019 Uke 14 . You can tailor the format pattern string to the output you want. Letters that should not be interpreted as format pattern letters go inside single quotes (apostrophes), here for example 'Uke' .

I am using java.time, the modern Java date and time API.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6 .

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

If you you cannot afford an external dependency (even on a rock solid and future-proof library), the old-fashioned SimpleDateFormat can be used in a similar way, but only after you have converted the selected values first to a Calendar and then to a Date . Be warned, however, that the SimpleDateFormat class is notoriously troublesome and also not thread-safe.

Links

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM