java.time
java.util 日期时间 API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用,改用modern Date-Time API*。
使用现代日期时间 API java.time 的解决方案:
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// A dummy Locale for the demo
Locale localeFromResponse = new Locale("default");
Locale locale = localeFromResponse.toString().equals("default") ? Locale.getDefault() : localeFromResponse;
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE, MMM d", locale);
String formatted = OffsetDateTime.now(ZoneOffset.UTC).format(dtf);
System.out.println(formatted);
// textView.setText(formatted);
}
}
输出:
Tue, Oct 5
ONLINE DEMO
如果你有一个现有的java.util.Calendar 对象
您可以使用Calendar#toInstant 将现有的java.util.Calendar 对象转换为java.time.Instant,而Calendar#toInstant 又可以转换为java.time API 的其他日期时间类型。
演示:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Dummy Locale and Calendar objects - for the demo
Locale localeFromResponse = new Locale("default");
Calendar calendar = Calendar.getInstance();
Locale locale = localeFromResponse.toString().equals("default") ? Locale.getDefault() : localeFromResponse;
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE, MMM d", locale);
ZonedDateTime zdt = calendar.toInstant().atZone(ZoneOffset.UTC);
String formatted = zdt.format(dtf);
System.out.println(formatted);
// textView.setText(formatted);
}
}
输出:
Tue, Oct 5
ONLINE DEMO
从 Trail: Date Time 了解有关现代日期时间 API 的更多信息。
以防万一您需要使用旧版 API 进行操作
无论出于何种原因,如果您想坚持使用旧版 API,您可以按照我在上面所做的相同方式映射 Locale,即它将是
Locale localeFromResponse = context.getResources().getConfiguration().locale;
Locale locale = localeFromResponse.toString().equals("default") ? Locale.getDefault() : localeFromResponse;
dateFormatDayMonth = new SimpleDateFormat("EEE, MMM d", locale);
textView.setText(dateFormatDayMonth.format(calendar.getTime()));
* 如果您正在为一个 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaring。请注意,Android 8.0 Oreo 已经提供了support for java.time。