Android dateFormat.parse(date) 我发送 01/03/2021 它显示 sun dec 27 00:00:00 GMT-05:00 2020

时间:2021-01-03 18:41:51

标签: java android date calendar add

我在使用该类时遇到问题,其中字符串日期的格式为 MM/dd/YYYY。布尔值是 true 添加一天,false 减去一天。

发送 01/03/2021 并且在 c.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;
}

1 个答案:

答案 0 :(得分:1)

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

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

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