store current date and date 1 year from current in java

If you do not want to drag external libraries, just use calendar.add(Calendar.YEAR, 1)

Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
cal.add(Calendar.YEAR, 1); // to get previous year add -1
Date nextYear = cal.getTime();

Note, if the date was 29/Feb/2012 and you added 1 year, you will get 28/Feb/2013


tl;dr

LocalDate.parse( 
    "2/18/2013" , 
    DateTimeFormatter.ofPattern ( "M/d/uuuu" ) 
).plusYears( 1 )

Details

The accepted Answer is correct, but outdated as of Java 8.

java.time

The java.time framework built into Java 8 and later supplants the troublesome old java.util.Date/.Calendar classes. The new classes are inspired by the highly successful Joda-Time framework, intended as its successor, similar in concept but re-architected. Defined by JSR 310. Extended by the ThreeTen-Extra project. See the Tutorial.

LocalDate

These new classes include LocalDate, to represent a date-only value with no time-of-day nor time zone.

First we must parse the String input. The java.time formatter uses pattern codes similar to the old classes, but not exactly the same. So be sure to read the new doc carefully.

Padded Zero

Notice that your input String lacks leading zero digit for some values. That means you should use single pattern codes, M and d rather than MM and dd. Double codes means you expect padding zero to always be included for otherwise single-digit values, 02 rather than 2.

String input = "2/18/2013";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "M/d/uuuu" );
LocalDate localDate = LocalDate.parse ( input , formatter );

Add a year. The java.time framework takes care of leap year.

LocalDate yearLater = localDate.plusYears ( 1 );

Dump to console.

System.out.println ( "localDate: " + localDate + " and yearLater: " + yearLater );

localDate: 2013-02-18 and yearLater: 2014-02-18


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


 Calendar cal = Calendar.getInstance();
        Date today = cal.getTime();
        cal.add(Calendar.YEAR, 1); // to get previous year add 1
        cal.add(Calendar.DAY_OF_MONTH, -1); // to get previous day add -1
        Date expiryDate = cal.getTime();


        System.out.println("today is --> "+today);
        System.out.println("expiryDate is --> "+expiryDate);

OutPut today is --> Fri Dec 01 10:10:52 GMT+05:30 2017

expiryDate is --> Fri Nov 30 10:10:52 GMT+05:30 2018

date today is -> Fri Dec 01 10:10:52 GMT+05:30 2017

expiryDate is -> Fri Nov 30 10:10:52 GMT+05:30 2018

enter image description here


Use Calendar#add(int field, int amount) method.You should use Calendar API in-order to manipulate date and time operations.

Calendar today = Calendar.getInstance(); 
Calendar nextYearToday = today;
nextYearToday.add(Calendar.YEAR, 1);

Tags:

Java

Date