有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

SimpleDataFormat中月份的java奇怪问题

我尝试设置日期和时间的格式:

Locale locale = context.getResources().getConfiguration().locale;
dateFormatDayMonth = new SimpleDateFormat("EEE, MMM d", locale);
textView.setText(dateFormatDayMonth.format(calendar.getTime()));

然而,结果是Sat, M10 2。我原以为是Sat, Oct 2。它以前是工作的,但突然停止了工作。 我错过了什么吗


共 (2) 个答案

  1. # 1 楼答案

    爪哇。时间

    java.util日期时间API及其格式化API SimpleDateFormat已经过时且容易出错。建议完全停止使用它们,并切换到modern Date-Time API*

    使用java.time现代日期时间API的解决方案:

    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对象

    可以使用^{}将现有的java.util.Calendar对象转换为java.time.Instant,而^{}又可以转换为java.timeAPI的其他日期时间类型

    演示:

    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 ^{}

  2. # 2 楼答案

    像这样初始化日期格式

    Locale locale = Locale.getDefault();
        DateFormat dateFormat = new SimpleDateFormat("EEE, MMM d", locale);
        Calendar calendar = Calendar.getInstance(locale);
        outputDate.setText(dateFormat.format(calendar.getTime()));