0

Hi i want to print the date in format "Monday 23rd April" in android textview also i would like to print previous days. for eg: "Sunday 22nd April", "Saturday 21st April". I have found a lot but was not able to get anything which would work in same format. Thanx in advance.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Sydroid
  • 509
  • 1
  • 8
  • 16
  • 2
    Your question suggests you've already *tried* something - so please tell us what you've tried and what happened. Where are you stuck? I *suspect* it's with the ordinal part ("rd", "st", "nd", "th" etc) - which is tricky to get right correctly across all locales. The rest should be reasonably straightforward. – Jon Skeet Mar 05 '14 at 06:56
  • yes, how to get the ordinal part..i have tried now String month_name=calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());//Locale.US); String day_name=calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault());//Locale.US); as suggested by @vinay maneti..and it works – Sydroid Mar 05 '14 at 07:04
  • @Sydroid - If that answer worked for you, then go ahead and accept it. :) Don't post that code here as a comment instead. – Rahul Mar 05 '14 at 07:07
  • How important is the ordinal part for you? That's likely to be a major inconvenience. Also, are you trying to support multiple locales, or just English? Multiple calendars, or just Gregorian? – Jon Skeet Mar 05 '14 at 07:08
  • only english and gregorian calendar... – Sydroid Mar 05 '14 at 07:14
  • possible duplicate of [Printing date in java with ordinal characters](http://stackoverflow.com/questions/8058279/printing-date-in-java-with-ordinal-characters). And this one, [How do you format the day of the month to say “11th”, “21st” or “23rd” in Java?](http://stackoverflow.com/q/4011075/642706). – Basil Bourque Mar 05 '14 at 11:34

2 Answers2

3

Take a look to: SimpleDateFormat

PS: To answer the question from Jon Skeet:

private static final String DATE_FORMAT_ND  = "EEE d'nd' MMMM";
private static final String DATE_FORMAT_RD  = "EEE d'rd' MMMM";
private static final String DATE_FORMAT_ST  = "EEE d'st' MMMM";
private static final String DATE_FORMAT_TH  = "EEE d'th' MMMM";
private static final String DATE_DAY_NUMBER = "d";

public static String FormatDate( Date date_ )
{
    String              format          = null;
    SimpleDateFormat    dayNumberFormat = new SimpleDateFormat( DATE_DAY_NUMBER );
    String              daySource       = dayNumberFormat.format( date_ );
    int                 day             = Integer.parseInt( daySource );

    switch( day )
    {
    case 1:
    case 21:
    case 31:
        format  = DATE_FORMAT_ST;
        break;

    case 2:
    case 22:
        format  = DATE_FORMAT_ND;
        break;

    case 3:
    case 23:
        format  = DATE_FORMAT_RD;
        break;

    case 4:
    case 24:
    case 5:
    case 25:
    case 6:
    case 26:
    case 7:
    case 27:
    case 8:
    case 28:
    case 9:
    case 29:
    case 10:
    case 30:
        format  = DATE_FORMAT_TH;
        break;

    }

    SimpleDateFormat    dateFormat  = new SimpleDateFormat( format );
    String              result      = dateFormat.format( date_ );

    return result;

}

There is only simple date format and a little bit of coding...

Call it like this for yesterdays date :- date.setText(FormatDate(new Date(new Date().getTime()-86400000)));

Sydroid
  • 509
  • 1
  • 8
  • 16
Maxim
  • 339
  • 1
  • 5
  • How is that going to produce the ordinal part ("nd" etc)? I don't *think* there's anything that will do that automatically. – Jon Skeet Mar 05 '14 at 07:01
  • See answer again... But from my point of view this resource for people who can write this code without any problems... – Maxim Mar 05 '14 at 07:32
  • Right, that's much better - it actually answers the question. Although it's probably worth explicitly noting that this is *only* suitable for English. (I'd specify that when constructing the `SimpleDateFormat`.) – Jon Skeet Mar 05 '14 at 08:38
  • I am afraid there is no common way especially for Chinese or Russian for example. – Maxim Mar 05 '14 at 08:58
  • Agreed (although icu4j may well provide a solution) - but I think it's important to make that clear in the answer. – Jon Skeet Mar 05 '14 at 09:12
  • date.setText(FormatDate(new Date(new Date().getTime()-86400000))); for yesterdays date – Sydroid Mar 05 '14 at 11:44
  • See Calendar (http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) methods setTime and roll for field DATE and getDate – Maxim Mar 05 '14 at 11:45
1

You could try this:

DateFormat format = new SimpleDateFormat("EEEE d MMMM yyyy");
System.out.println(format.format(new Date()));

Or check out this Human Friendly dates

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44