Christos Baziotis
Christos Baziotis

Reputation: 6035

Android - format Date to "Day, Month dd, yyyy"

I get from the user, the date he sets in a DatePickerDialog. The date i get is in this format:

int selectedYear, int selectedMonth, int selectedDay

Can i format it to be like this "Day, Month dd, yyyy" as shown in the picture below?

enter image description here

Upvotes: 6

Views: 22842

Answers (3)

Baschi
Baschi

Reputation: 1128

Use a SimpleDateFormat to format the date:

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, selectedYear);
    cal.set(Calendar.MONTH, selectedMonth);
    cal.set(Calendar.DAY_OF_MONTH, selectedDay);
    SimpleDateFormat sdf = new SimpleDateFormat();
    String DATE_FORMAT = "EE, MMM dd, yyyy";
    sdf.applyPattern(DATE_FORMAT);
    String formattedDate = sdf.format(cal.getTime());

Upvotes: 1

dafi
dafi

Reputation: 3522

Using the values returned from picker

    int selectedYear = 2013;
    int selectedDay = 20;
    int selectedMonth = 11;

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, selectedYear);
    cal.set(Calendar.DAY_OF_MONTH, selectedDay);
    cal.set(Calendar.MONTH, selectedMonth);
    String format = new SimpleDateFormat("E, MMM d, yyyy").format(cal.getTime());

Upvotes: 28

Subramanian Ramsundaram
Subramanian Ramsundaram

Reputation: 1347

You can Try This:

SimpleDateFormat sdf = new SimpleDateFormat("EEE-dd-MM-yyyy"); // Set your date format
String currentData = sdf.format(your actual date); // Get Date String according to date format

Here you can see details and all supported format:

http://developer.android.com/reference/java/text/SimpleDateFormat.html

Upvotes: 6

Related Questions