Java Data Type How to - Increment date (timestamp)


We would like to know how to increment date (timestamp).


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
//  w ww. j a  va2  m
public class Main {

  public static void main(String[] args) {
    System.out.println(getDateAsStringIncrementedByYear("04/22/1980 11:30:20",
        "MM/dd/yyyy HH:mm:ss", 1));

  private static String getDateAsStringIncrementedByYear(String inputDateStr,
      String pattern, long yearsToIncrement) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
    LocalDateTime dateTime = LocalDateTime.parse(inputDateStr, formatter);
    return dateTime.plusYears(yearsToIncrement).format(formatter);

The code above generates the following result.