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

微笑、不失礼 提交于 2020-02-02 03:05:50

问题


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?


回答1:


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());



回答2:


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:


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());


来源:https://stackoverflow.com/questions/19928757/android-format-date-to-day-month-dd-yyyy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!