24

Let's say I have this:

 PrintStream out = System.out;
 Scanner in = new Scanner(System.in);
 out.print("Enter a number ... ");
 int n = in.nextInt();

I have a random date, for example, 05/06/2015 (it is not a fixed date, it is random every time). If I want to take the 'year' of the this date, and add whatever 'n' is to this year, how do i do that?

None of the methods in the Date Class are 'int'.

And to add years from an int, 'years' has to be an int as well.

Lefty G Balogh
  • 1,771
  • 3
  • 26
  • 40
Shawn
  • 309
  • 2
  • 5
  • 11

9 Answers9

59

You need to convert the Date to a Calendar.

Calendar c = Calendar.getInstance();
c.setTime(randomDate);
c.add(Calendar.YEAR, n);
newDate = c.getTime();

You can manipulate the Year (or other fields) as a Calendar, then convert it back to a Date.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • what if my date is SimpleDateFormat class? for c.setTime(randomDate); what do i do? – Shawn Jul 25 '12 at 04:38
  • 1
    @user1519192 Whenever you get a String that you want to parse into a Date, you can the `parse()` method of your `SimpleDateFormat`. A SimpleDateFormat is not a date, it is a definition of a format that a Date can represent and is used to convert a `Date` to a `String` and vice versa. – Jon Lin Jul 25 '12 at 04:42
21

This question has long deserved a modern answer. And even more so after Add 10 years to current date in Java 8 has been deemed a duplicate of this question.

The other answers were fine answers in 2012. The years have moved on, today I believe that no one should use the now outdated classes Calendar and Date, not to mention SimpleDateFormat. The modern Java date and time API is so much nicer to work with.

Using the example from that duplicate question, first we need

private static final DateTimeFormatter formatter 
        = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");

With this we can do:

    String currentDateString = "2017-09-12 00:00:00";
    LocalDateTime dateTime = LocalDateTime.parse(currentDateString, formatter);
    dateTime = dateTime.plusYears(10);
    String tenYearsAfterString = dateTime.format(formatter);
    System.out.println(tenYearsAfterString);

This prints:

2027-09-12 00:00:00

If you don’t need the time of day, I recommend the LocalDate class instead of LocalDateTime since it is exactly a date without time of day.

    LocalDate date = dateTime.toLocalDate();
    date = date.plusYears(10);

The result is a date of 2027-09-12.

Question: where can I learn to use the modern API?

You may start with the Oracle tutorial. There’s much more material on the net, go search.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
9

Another package for doing this exists in org.apache.commons.lang3.time, DateUtils.

Date date = new Date();
date = DateUtils.addYears(date, int quantity = 1);
user1023102
  • 189
  • 3
  • 12
5

The Date class will not help you, but the Calendar class can:

Calendar cal = Calendar.getInstance();
Date f;
...
cal.setTime(f);
cal.add(Calendar.YEAR, n); // Where n is int
f = cal.getTime();

Notice that you still have to assign a value to the f variable. I frequently use SimpleDateFormat to convert strings to dates.

Hope this helps you.

Barranka
  • 20,547
  • 13
  • 65
  • 83
1

Try java.util.Calendar type.

Calendar cal=Calendar.getInstance();
cal.setTime(yourDate.getTime());
cal.set(Calendar.YEAR,n);
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

This will add 3 years to the current date and print the year.

 System.out.println(LocalDate.now().plusYears(3).getYear());
0

If you need add one year a any date use the object Calendar.

Calendar dateMoreOneYear = Calendar.getInstance();
dateMoreOneYear.setTime(dateOriginal);
dateMoreOneYear.add(Calendar.DAY_OF_MONTH, 365);
horelvis
  • 125
  • 5
0

Try like this as well for a just month and year like (June 2019)

Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, n); //here n is no.of year you want to increase
SimpleDateFormat format1 = new SimpleDateFormat("MMM YYYY");
System.out.println(cal.getTime());

String formatted = format1.format(cal.getTime());
System.out.println(formatted);
Bhaskara Arani
  • 1,556
  • 1
  • 26
  • 44
-2

Try this....

String s = new SimpleDateFormat("YYYY").format(new Date(random_date_in_long)); // 
int i = Integer.parseInt(s)+n;
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75