当前位置: 首页 > 知识库问答 >
问题:

可以从当前日期添加150天吗?

萧浩漫
2023-03-14

我有一个像20211231这样的当前日期格式,我想在这个日期上再加150天。在那之后,如果时间还不到150天,或者已经过去了,我怎么检查呢

我尝试这个代码添加150天到当前日期,但它失败了

 Calendar c= Calendar.getInstance();
        c.add(Calendar.DATE, 150);
        Date d = c.getTime ();

idk如果它不再有效了,或者自从我读了一个9年零五个月前被问和回答的问题后又怎么样https://stackoverflow.com/a/11727986/17562398

这是我现在的约会对象

 SimpleDateFormat cdate = new SimpleDateFormat ("yyyyMMdd", Locale.getDefault ( ));
        String currentDate = cdate.format (new Date ( ));

共有2个答案

西门飞翮
2023-03-14

这是我用currentdate(今天)检查150天的逻辑

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

 String sDate="20211231"; 
 Date cuDate=dateFormat.parse(sDate);  

Date currentDate = new Date();
 Calendar calendarObj = Calendar.getInstance();
        calendarObj.setTime(cuDate);

calendarObj.add(Calendar.MONTH, 5); // since this is 150 days (i think it's 30 days * 5 )

Date newDateAfter5months = calendarObj.getTime();


System.out.println("newDateAfter5months="+dateFormat.format(newDateAfter5months));


if(newDateAfter5months.compareTo(currentDate) > 0) {
         System.out.println("newDateAfter5months occurs after currentDate");
      } 
能向晨
2023-03-14

使用来自java.time.*包的现代JavaTime API。

可以使用java以所需格式解析或格式化(用于显示)日期。时间总体安排DateTimeFormatter。模式(字符串、区域设置)方法。以下是DateTimeFormatter类的文档

DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd", locale);

如果解析的是用户输入的日期,也不要忘记捕获解析异常。

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

//...

// The desired locale, maybe the target user selected language/region locale?
// For now just using the default locale here
Locale locale = Locale.getDefault();
    
String someInputDate = "20220529";
    
try {
    // using the DateTimeFormatter to parse the date with a desired pattern
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd", locale);
    LocalDate inputDate = LocalDate.parse(someInputDate, dateFormatter);
    
    // 150 days from now
    int daysFromNow = 150;
    LocalDate targetDate = LocalDate.now().plusDays(daysFromNow);
    android.util.Log.d(
        "Dates", String.format("The date %d days from now is %s", daysFromNow, targetDate.format(dateFormatter))
    );
    if (inputDate.isAfter(targetDate)) {
        // the input date is past 150 day from now
        android.util.Log.d(
            "Dates", String.format("%s at least %d days from now", inputDate.format(dateFormatter), daysFromNow)
        );
    } else {
        // the input date is NOT past 150 day from now
        android.util.Log.d(
            "Dates", String.format("%s is less than %d days from now", inputDate.format(dateFormatter), daysFromNow)
        );    
    }
} catch (DateTimeParseException e) {
    // The input is a date in a different format or is malformed/invalid
}

日志输出

Dates    The date 150 days from now is 20220530
Dates    20220529 is less than 150 days from now
 类似资料:
  • 我有一个像20211231这样的当前日期格式,我想在这个日期上再加150天。在那之后,如果时间还不到150天,或者已经过去了,我怎么检查呢 我尝试用这段代码将当前日期增加150天,但失败了 idk如果它不再有效了,或者自从我读了一个9年零五个月前被问和回答的问题后又怎么样https://stackoverflow.com/a/11727986/17562398 这是我现在的日期选择器

  • 问题内容: 有人可以告诉我如何从当前日期减去45天吗? 谢谢 问题答案: 您可以使用Calendar类:

  • 我试图从模型“用户”中提取对象,其日期从今天算起已经超过30天了。 碳::现在()== 如何做到这一点?

  • 我不确定这是否可能? 我得拿到密码才能读 今天的月份即2月今天的日期即17月底的日期即31日,然后是一年中的其他11个月,在一行中按顺序排列 8月1日-31日|9月|10月|11月|12月|1月|2月|3月|4月|5月|6月|7月 请以最佳方式提供任何建议? 多谢 定时(timing的缩写)

  • 问题内容: 我有一个当前的Date对象,需要使用JavaScript Date对象将其增加一天。我有以下代码: 如何添加一天? 我已将+1添加到,但不会显示“”的“星期日” ,这是我希望发生的情况。 问题答案: 要将一天添加到日期对象:

  • 我可以打印当前日期如何使用date对象从当前日期减去2天。无论之前在这个网站上问了什么问题,都不符合我的要求。如果我使用日历,那么我会得到不同格式的日期。我只希望使用date对象以yyyyMMdd格式显示日期。我尝试了这个方法,但我遇到了错误,就像运算符一样——参数类型字符串int未定义。请帮助我解决这个问题。