使用 SimpleDateFormat 转换 Android 中的日期格式

Converting date formats in Android using SimpleDateFormat

我正在使用以下代码从 DatePicker:

转换日期格式
public void onDateSet(DatePicker view, int year, int month, int day) {
    System.out.println("year=" + year + "day=" + day + "month="
            + month);
    String myFormat = "dd-M-yyyy";
    String dateStr = day + "-" + month + "-" + year;

    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);
    SimpleDateFormat originalFormat = new SimpleDateFormat(myFormat, Locale.ENGLISH);
    SimpleDateFormat targetFormat = new SimpleDateFormat("dd,MMMM yyyy");
    Date date = originalFormat.parse(dateStr);
    String formattedDate = targetFormat.format(date);  
    System.out.println(formattedDate);
}

不幸的是它抛出了一个错误:

Error:(115, 45) error: unreported exception ParseException; must be caught or declared to be thrown

我试着给它添加一个 throws:

public void onDateSet(DatePicker view, int year, int month, int day) throws ParseException {
    System.out.println("year=" + year + "day=" + day + "month="
            + month);
    String myFormat = "dd-M-yyyy";
    String dateStr = day + "-" + month + "-" + year;
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);
    SimpleDateFormat originalFormat = new SimpleDateFormat(myFormat, Locale.ENGLISH);
    SimpleDateFormat targetFormat = new SimpleDateFormat("dd,MMMM yyyy");
    String effDate = targetFormat.format(originalFormat.parse(dateStr));
    System.out.println(effDate);
}

新错误显示为

Error:(106, 21) error: onDateSet(DatePicker,int,int,int) in DatePickerFragment cannot implement onDateSet(DatePicker,int,int,int) in OnDateSetListener, overridden method does not throw ParseException

出现这个错误是因为parse method throws a ParseException - this exception is a checked one, and it needs to be handled properly: by putting the code in a try/catch block or declaring that the method onDateSet throws the exception(在方法声明中加入throws ParseException)。 在你的例子中,因为你似乎在扩展一个 class 或实现一个接口,只需将代码放在一个 try/catch 块中。

另一个细节是您不需要创建 3 个 SimpleDateFormat 实例。一个就够了,输出 (targetFormat).

要构建日期对象,只需使用 java.util.Calendar - 提醒在此 API、月份从零开始 (是的,一月是 0,因此您可能必须减去 1 - 检查您的 API 是否正确获取月份):

Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, day);

SimpleDateFormat targetFormat = new SimpleDateFormat("dd,MMMM yyyy", Locale.ENGLISH);
String formattedDate = targetFormat.format(cal.getTime());

新建Date/timeAPI

旧的 classes(DateCalendarSimpleDateFormat)有 lots of problems and design issues,它们正在被新的 APIs.

如果您可以在项目中添加依赖项,在 Android 中,您可以使用 ThreeTen Backport, a great backport for Java 8's new date/time classes, together with the ThreeTenABP (more on how to use it ).

所有相关的 class 都在包 org.threeten.bp 中。代码要简单得多,您不必担心索引为零的月份(API 中的一月是 1):

import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.LocalDate;

DateTimeFormatter targetFormat = DateTimeFormatter.ofPattern("dd,MMMM yyyy", Locale.ENGLISH);
String formattedDate = targetFormat.format(LocalDate.of(year, month, day));