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 parth295
• 4,640 points
693 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 2, 2018 by sharth
• 3,350 points

edited May 2, 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,880 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,150 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,190 points
607 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 in Java by Sirajul
• 39,540 points
121 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
89 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,050 points
847 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,540 points
8,172 views
0 votes
1 answer

Measure time elapsed in Java?

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

answered Jul 3, 2018 in Java by scarlett
• 1,280 points
52 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 parth295
• 4,640 points
69 views
0 votes
2 answers

Fetch list of in-between dates using Java

java.time Package The new java.time.package in Java 8 incorporates ...READ MORE

answered Aug 21 in Java by Sirajul
• 39,540 points
138 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 in Java by Sirajul
• 39,540 points
112 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,350 points
89 views