2

I'm currently learning how to create applications for Android, but my Java is quite rusty as I'm more of a .NET person.

If in C#, I wanted to create a DateTime object with the value set to todays date plus 5 years, I could use

DateTime dt = DateTime.Now.AddYears(5);

Is there something similar to this in the Java language?

2 Answers 2

7

You could use a Calendar to do the calculation:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 5);
Date date = cal.getTime(); // getTime() returns a Date object
Sign up to request clarification or add additional context in comments.

Comments

2

you can use JODA time --- http://joda-time.sourceforge.net to create DateTime object.

Use plusYears method to add 5 years to it -- http://joda-time.sourceforge.net/api-release/org/joda/time/DateTime.html

 DateTime.now().plusYears(5);

1 Comment

I'll bear this in mind for future projects as it looks great, but for now this seems a bit like overkill! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.