简体   繁体   中英

How do I convert my date string to a custom date format?

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());
    }

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()));
}

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

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