0

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 ?

  • 21? Does the date picker give you 21 for 2021, or did you pick a date in year 21 CE? Both sound very unlikely to me. – Ole V.V. Aug 20 '21 at 16:56

2 Answers2

1

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

MriDx
  • 61
  • 1
  • 6
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android look into [desugaring](https://developer.android.com/studio/write/java8-support-table). – Ole V.V. Aug 20 '21 at 16:53
0

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161