Android God
Android God

Reputation: 79

Android date picker showing incorrect format

I'm having trouble getting the right format here. I'm trying to get a proper date from my android date picker to shove into a date object.

My Code

                final android.icu.util.Calendar cldr = android.icu.util.Calendar.getInstance();
            int day = cldr.get(android.icu.util.Calendar.DAY_OF_MONTH);
            int month = cldr.get(android.icu.util.Calendar.MONTH);
            int year = cldr.get(android.icu.util.Calendar.YEAR);

            picker = new DatePickerDialog(getContext(),
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                            txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                        }
                    }, year, month, day);
            picker.show();

The result is 16-08-21(d-mm-yy) but I needed 16-Aug-21 (dd-mmm-yy)

Can you please help me out on the same ?

Upvotes: 0

Views: 521

Answers (2)

Anonymous
Anonymous

Reputation: 86232

java.time

You tagged your question java-8, so I suppose you want the Java 8 solution. Use java.time, the modern Java date and time API introduced in Java 8, for your date work.

Unfortunately your date picker uses month numbers 0 through 11 as a remainder from the outdated Calendar class. So in our code we need to add 1.

    int year = 2021;
    int monthOfYear = Calendar.AUGUST; // Don’t use Calendar in your code; for demonstration only
    int dayOfMonth = 16;
    
    LocalDate date = LocalDate.of(year, monthOfYear + 1, dayOfMonth);
    String dateString = date.format(DATE_FORMATTER);

    System.out.println(dateString);

I have used this static formatter:

private static final DateTimeFormatter DATE_FORMATTER
        = DateTimeFormatter.ofPattern("dd-MMM-yy", Locale.ENGLISH);

Output from this snippet is:

16-Aug-21

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both 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 non-Android 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 either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Upvotes: 0

MriDx
MriDx

Reputation: 61

parse date string to date object and then format to dd-MMM-yy

    String dateString = "16-08-21";

    Date parsedDate = SimpleDateFormat("dd-MM-yy").parse(dateString);

    String formattedDateString = SimpleDateFormat("dd-MMM-yy").format(parsedDate);

    Log.d("mridx", "onCreate:" +parsedDate);

    Log.d("mridx", "onCreate:" + formattedDateString);

logs

D/mridx: onCreate: Mon Aug 16 00:00:00 GMT+05:30 2021

D/mridx: onCreate: 16-Aug-21

Upvotes: 1

Related Questions