-->

Date Increment in java

How can we increment a date by one hour in Java?

We have created small java program for calculating the date.

In below java program we are increment the date from 12-Dec-2018 to 12-Jan-2019
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;</code>
 
public class DateIncrement {
 
private static String startingdate ="2018-12-12";
private static String endingdate ="2019-01-12";
 
public static void main(String[] args) {
 
try {
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
 
 Date startDate = formatter.parse(startingdate);
 Date endDate = formatter.parse(endingdate);
 
 Calendar start = Calendar.getInstance();
 start.setTime(startDate);
 Calendar end = Calendar.getInstance();
 end.setTime(endDate);
 
 for (Date date = start.getTime(); start.before(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
  String newstartdatetime =formatter.format(date);
  System.out.println("New Date After Incriment : "+newstartdatetime);
 }
 
  } catch (Exception e) {
  e.printStackTrace();
 }
} 
}

No comments:

Post a Comment