Search code examples
androidjava.util.date

How to display current date in text format on textview in android using android.text.format?


I am getting result as 12/1/2012 but I want to display as "Saturday December 1,2012" my code is as below please give me a solution if you have......

Date date = new Date();
myDateString = DateFormat.getDateFormat(getApplicationContext()).format(date).toString();
dateTxt.setText(myDateString);

Solution

  • See this Example

     String[] formats = new String[] {
       "yyyy-MM-dd",
       "yyyy-MM-dd HH:mm",
       "yyyy-MM-dd HH:mmZ",
       "yyyy-MM-dd HH:mm:ss.SSSZ",
       "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
     };
     for (String format : formats) {
       SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
       System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
       sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
       System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
     }
    

    Which produces this output when run in the PDT time zone:

                     yyyy-MM-dd 1969-12-31
                     yyyy-MM-dd 1970-01-01
               yyyy-MM-dd HH:mm 1969-12-31 16:00
               yyyy-MM-dd HH:mm 1970-01-01 00:00
              yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
              yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
       yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
       yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
     yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
     yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000
    

    So use this format:

    "EEEE, MMMM dd, yyyy" -> "Monday, April 6, 1970"
    

    See these references for complete detail

    1. http://developer.android.com/reference/android/text/format/DateFormat.html
    2. http://developer.android.com/reference/java/text/SimpleDateFormat.html

      import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale;

      String Format = "EEEE, MMMM dd, yyyy";

      Date today = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(Format, Locale.ENGLISH); System.err.format("%30s %s\n", Format, sdf.format(today));

    This is printing EEEE, MMMM dd, yyyy Saturday, December 01, 2012

    Check your imports and if problem persists Please try to post your new code.