Android - format Date to “Day, Month dd, yyyy”

2019-04-29 08:53发布

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

3条回答
神经病院院长
2楼-- · 2019-04-29 09:37

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

查看更多
淡お忘
3楼-- · 2019-04-29 09:40

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());
查看更多
够拽才男人
4楼-- · 2019-04-29 09:44

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());
查看更多
登录 后发表回答