Increment the date in Java by 1-day

0 votes
I want to increase the date in Java by 1. Can we do that?If Yes what is the simplest way to do that?
May 2, 2018 in Java by Parth
• 4,640 points
4,538 views

3 answers to this question.

0 votes

You can try this way

SimpleDateFormat simpleDateFormat1 = n`ew SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.set(1970,Calendar.NOVEMBER,31);
System.out.println(simpleDateFormat1.format(calendar.getTime()));
calendar.add(Calendar.DATE, 1);
System.out.println(simpleDateFormat1.format(calendar.getTime()));
calendar.add(Calendar.DATE, -1);
System.out.println(simpleDateFormat1.format(calendar.getTime()));

Will Print:

1970-11-31
1971-12-01
1970-11-31
answered May 3, 2018 by sharth
• 3,370 points

edited May 3, 2018 by sharth
0 votes
public class DateUtil
{
    public static Date addDays(Date date, int days)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days); //minus number would decrement the days
        return cal.getTime();
    }
}


public class DateUtil
{
    public static Date addDays(Date date, int days)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days); //minus number would decrement the days
        return cal.getTime();
    }
}
answered Jul 31, 2018 by Sushmita
• 6,920 points
0 votes
import java.time.LocalDate;

public class DateIncrementer {
  static public String addOneDay(String date) {
    return LocalDate.parse(date).plusDays(1).toString();
  }
}
answered Aug 1, 2018 by Akrati
• 3,190 points

Related Questions In Java

+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,588 views
0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,230 points
2,941 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
845 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,109 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,620 points
27,618 views
0 votes
1 answer

Measure time elapsed in Java?

This is also a way to work ...READ MORE

answered Jul 4, 2018 in Java by scarlett
• 1,290 points
854 views
0 votes
1 answer

How to get the current date/time in Java?

It depends on what form of date/time ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,770 points
1,267 views
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by Parth
• 4,640 points
963 views
0 votes
2 answers

Get all the permutations of a string in Java

You could use recursion to do this.  Try ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
2,254 views
0 votes
1 answer

How to split Strings by space in Java ?

You can use split() method. str = "Hello ...READ MORE

answered May 16, 2018 in Java by sharth
• 3,370 points
1,581 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP