-1

I am currently working on creating a custom date format such that it is supposed to show as "Monday, April 8, 2019 " but whenever I pick from dialog and apply simple date format, it returns the date with abbreviated month and time (which I don't need). any idea how I can update my code to get it to work in the above format? Here's my code :

@Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

        String date = (++monthOfYear)+" "+dayOfMonth+", "+year;
        SimpleDateFormat dateFormat = new SimpleDateFormat("E MMM dd yyyy");
        dateFormat.format(new Date());
        Date convertedDate = new Date();
        try {
            convertedDate = dateFormat.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        myDate.setText(convertedDate.toString());
    }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • personally i prefer creating my own custom dates using my own months either abbreviated or not create getMonthFromInt(dayOfMonth) – Master Fathi Apr 08 '19 at 16:08
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Apr 09 '19 at 05:18
  • Possible duplicate of [display Java.util.Date in a specific format](https://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format) – Ole V.V. Apr 09 '19 at 05:22
  • Or possible duplicate of [Change date format of the DatePickerDialog Calendar in Android](https://stackoverflow.com/questions/39046113/change-date-format-of-the-datepickerdialog-calendar-in-android) – Ole V.V. Apr 09 '19 at 06:28

2 Answers2

0

To achieve such a format: Monday, April 8, 2019 you have to format the date like this (source):

SimpleDateFormat mDateFormat = new SimpleDateFormat("EEEE, MMMMM dd, yyyy");
myDate.setText(mDateFormat.format(convertedDate));

Good luck


I would do something like this:

@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
    Calendar mDate = Calendar.getInstance();
    mDate.set(year, monthOfYear, dayOfMonth);
    SimpleDateFormat mDateFormat = new SimpleDateFormat("EEEE, MMMMM dd, yyyy");
    myDate.setText(mDateFormat.format(mDate.getTime()));
}
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • this doesnt seem to work ,In my code I am just replacing the line to your suggested "SimpleDateFormat mDateFormat = new SimpleDateFormat("EEEE, MMMMM dd, yyyy"); " but it breaks it in the sense, it shows only todays date instead of the picked date. Also mydate settext is expecting a string so it crashes for myDate.setText(mDateFormat.format(convertedDate)); any ideas what could be wrong? –  Apr 08 '19 at 15:45
  • i used your approach and it crashed with E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myapp, PID: 28523 java.lang.IllegalArgumentException: Cannot format given Object as a Date at java.text.DateFormat.format(DateFormat.java:306) at java.text.Format.format(Format.java:157) at com.ui.registervisitor.RegisterVisitorFragment.onDateSet on the myDate line –  Apr 08 '19 at 16:35
  • something strange is going on with the above code in place for onDateSet function.It shows the correct format and correct day and year but it shows wrong day of the week (EEEE) and wrong month (MMMM). It actually shows me may for month even if i pick april and shows me saturday when its a tuesday –  Apr 09 '19 at 14:24
  • 1
    actually the above code works with a missing line : mDateFormat.parse(date); that made it perfect! thanks! also the simpledateformatpattern i used to get it to work is : "EEE, MMMM d, yyyy" –  Apr 09 '19 at 15:02
0

java.time and ThreeTenABP

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
            .withLocale(Locale.US);
    LocalDate date = LocalDate.of(year, ++monthOfYear, dayOfMonth);
    String formattedDate = date.format(dateFormatter);

Monday, April 8, 2019

Don’t bother with defining you own date format through a format pattern string. The format you want is already built in.

I am using java.time, the modern Java date and time API. The date/time clases that you tried to use, SimpleDateFormat and Date, are long outdated and poorly designed, the former in particular notoriously troublesome, so I didn’t want to use those.

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.

What went wrong in your code?

  • First, you are assembling the incoming year, monthOfYear and dayOfMonth into a string that you are trying to parse, which is the detour, but worse, the string hasn’t got format E MMM dd yyyy, so parsing it with your SimpleDateFormat probably failed. If you haven’t seen the stacktrace from e.printStackTrace();, I suggest that you’ve got a serious problem in your project setup that you should fix before writing another code line. If you don’t see the errors that happen in your code, you are coding blindfolded. The parse error caused the current date obtained from new Date() to be displayed instead.
  • convertedDate.toString() returns a string in the format EEE MMM dd HH:mm:ss zzz yyyy to be displayed. For example Tue Apr 09 09:39:47 EDT 2019. Date.toString always returns this format. It has nothing to do with the format the date was parsed from (even if it had been successfully parsed) since a Date is just a point in time, it cannot have a format in it.

Links

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