Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

numberformatexception - How to get the next date using java with present date as input

For my need, I have to update the date to the next day given I have the current date

The method I am using to get the current date


       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT-4:00"));
        return sdf.format(new Date());

Now I want to get the next date using this as input I tried this

  System.out.println(Integer.parseInt(getOrderDate())+1);

I am getting this error

java.lang.NumberFormatException: For input string: "2020-01-06T09:46:29-0400

Can someone help and let me know.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to add the day where you set the date :

    return sdf.format(new Date());

To do so, you can have a method like :

public String getOrderDate(Integer daysToAdd) {
    // Date computation
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    if(daysToAdd!=null) {
       c.add(Calendar.DATE, daysToAdd);
    }
    // Date formatting
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT-4:00"));
    return sdf.format(c.getTime());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...