【问题标题】:Android dateFormat.parse(date) I send 01/03/2021 it shows sun dec 27 00:00:00 GMT-05:00 2020 [duplicate]Android dateFormat.parse(date) 我发送 01/03/2021 它显示 sun dec 27 00:00:00 GMT-05:00 2020 [重复]
【发布时间】:2021-04-09 16:58:58
【问题描述】:

我遇到了字符串日期格式为MM/dd/YYYY 的此类问题。布尔值是 true 增加一天,false 减少一天。

发送01/03/2021c.setTime(dateFormat.parse(date));,得到的值为Sun "Dec 27 00:00:00 GMT-05:00 2020"。我不明白这是什么问题。

public static String dateDay(String date,boolean add){
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY");
    Calendar c = Calendar.getInstance();
    try {
        c.setTime(dateFormat.parse(date));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    if(add)
        c.add(Calendar.DAY_OF_YEAR,1);
    else
        c.add(Calendar.DAY_OF_YEAR,-1);
    dateFormat=new SimpleDateFormat("MM/dd/YYYY");
    Date newDate=new Date(c.getTimeInMillis());
    String resultDate=dateFormat.format(newDate);
    return resultDate;
}

【问题讨论】:

  • 这是用什么语言写的?我说不出来。
  • 顺便考虑扔掉早已过时且臭名昭著的麻烦SimpleDateFormat和朋友。看看您是否可以使用desugaring 或将ThreeTenABP 添加到您的Android 项目中,以便使用现代Java 日期和时间API 的java.time。使用起来感觉好多了。
  • @MarkGiraffe 它是 Java(Android 开发中比较流行的语言之一)(我添加了语言标签)。

标签: java android date calendar add


【解决方案1】:

SimpleDateFormat() 使用小写 yyyy 表示年份而不是大写。 换行

dateFormat=new SimpleDateFormat("MM/dd/YYYY");

dateFormat=new SimpleDateFormat("MM/dd/yyyy");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    相关资源
    最近更新 更多