在Android中更改日期格式

时间:2013-11-18 06:34:19

标签: android android-datepicker android-date

我在我的应用中使用日历,我想更改日期的格式。

private void updateLabel() {
        String myFormat = "dd/MM/yy"; // In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

            checkin_date.setText(sdf.format(myCalendar.getTime()));


    }

我想要这种格式Fri, 21 Nov 2013

我们怎么能这样做。请帮助我

3 个答案:

答案 0 :(得分:1)

此处在您需要执行的更新中

private void updateLabel() {
        String myFormat = "EEE,dd-MM-yyyy"; // In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        checkin_date.setText(sdf.format(myCalendar.getTime()));
    }

答案 1 :(得分:0)

试试这个:

SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
SimpleDateFormat desiredFormat =new SimpleDateFormat("EEE,dd-MMM-yyyy");    
Date dt = sourceFormat.parse(your_date);
String desiredDateString = desiredFormat.format(dt)

答案 2 :(得分:0)

您可以使用方法:

private String changeFormat(String unformattedDate, SimpleDateFormat formatFrom, SimpleDateFormat formatTo) {
    if (TextUtils.isEmpty(unformattedDate)) {
        return "";
    }
    try {
        Date date = formatFrom.parse(unformattedDate);
        return formatTo.format(date);
    } catch (ParseException e) {
        throw new IllegalArgumentException(String.format("Date '%s' has incorrect format", unformattedDate));
    }
}

示例:

SimpleDateFormat formatFrom = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat formatTo = new SimpleDateFormat("EEE, dd MMM yyyy");
String sourceDate = "18/11/2013";
String desiredDate = changeFormat(sourceDate, formatFrom, formatTo);