Search code examples
androiddatetimestring-formattingjodatimedatetime-format

Joda - DateTimeFormatter - print day of a week as a shortcut


I'm using DateTimeFormatter:

DateTimeFormatter dateTimeFormatter = DateTimeFormat.fullDate();
dateTimeFormatter.print(...);

I need to print full date but the day of a week should be displayed as a shortcut. Is it possible?


Solution

  • Your best bet here is to inspect the format of DateTimeFormat.fullDate() and then reconstruct it yourself using a pattern. In my Locale (English/Australia):

    DateTimeFormatter dtf = DateTimeFormat.fullDate();
    System.out.println(dtf.print(DateTime.now()));
    //result: Sunday, May 7, 2017
    

    So I would use the following pattern to get the abbreviated day of week:

    DateTimeFormatter dtf = DateTimeFormat.forPattern("E, MMM d, YYYY");
    System.out.println(dtf.print(DateTime.now()));
    //result: Sun, May 7, 2017
    

    Note that "E" and "EEE" are the two patterns for abbreviated day of week and full day of week. Refer to the javadoc for DateTimeFormat for a list of patterns