Calculate a date 100 days after a date in Java

Source: Internet
Author: User
Tags time and date

Using Java to calculate datetime, first consider the time class, Java provides in the time and date related classes, there is a calendar class, you can get a time and date.

To calculate the date 100 days after a certain date, the idea is as follows:

1. Set the time first, e.g. January 1, 2017;

2. Increase the number of days on a given date by 100;

The specific code is as follows:

1         /*2 * 1, first given a time, such as January 1, 20173          */4Calendar CLD =calendar.getinstance ();5Cld.set (Calendar.year, 2017);6Cld.set (calendar.monday,0);7Cld.set (calendar.date,1);8         9         //call Add () in the Calendar class to increase the amount of timeTenCld.add (calendar.date, 100); One          ASystem.out.println ("Add 100 Days to Date:" +cld.get (calendar.year) + "year" +cld.get (calendar.month) + "Month" +cld.get (calendar.date) + "Day"); -          -         

The printing results are as follows:

Add 100 days to date: March 11, 2017

Adds or subtracts a specified amount of time for a given calendar field, based on the rules of the calendar. For example, to subtract 5 days from the current calendar time, you can do this by calling the following method:

add(Calendar.DAY_OF_MONTH, -5)

Parameters:
field -Calendar field.
amount -The amount of date or time added to the field.

Calculate a date 100 days after a date in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.