Java #43: How to add Days, Months, and Years to a LocalDate in Java

——-Source Screenshot————

——-Full Source Code————–
//Programmer: Akinwale Owi
//Company: Dagba Computers
//Program Purpose: Adding days, months, and years to a LocalDate

package dagbacomputers;

/**
*
* @author Akin
*/

//imports
import java.time.LocalDate;

public class DagbaComputers {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Creating the date
LocalDate gameRelease = LocalDate.of(2017, 5, 18);

//Turing the day of the month into an integer
int day = gameRelease.getDayOfMonth();

//Turning month into an integer
int month = gameRelease.getMonthValue();

//Turing year into an integer
int year = gameRelease.getYear();

//Printing all the values out together
System.out.printf(“The Game Release Date: ” + month + “,” + day + “,” + year + “\n”);

//Creating a LocalDate object for 50 days later
LocalDate fiftyLater = gameRelease.plusDays(50);

//Creating the day integer
int daysLater = fiftyLater.getDayOfMonth();

//Creating the month integer
int monthsLater = fiftyLater.getMonthValue();

//Creating the year value
int yearsLater = fiftyLater.getYear();

//Printing out the values to test
System.out.printf(“50 days later is: ” + monthsLater + “,” + daysLater + “,” + yearsLater + “\n”);

//Creating new LocalDate for 3 months later
LocalDate threeMonths = gameRelease.plusMonths(3);

//Creating three months day value
int dayMonths = threeMonths.getDayOfMonth();

//Creating three months month value
int monthMonths = threeMonths.getMonthValue();

//Creating three month’s year value
int yearMonths = threeMonths.getYear();

//Printing out three months value
System.out.printf(“Three months later: ” + monthMonths + “,” + dayMonths + “,” + yearMonths + “\n”);

//Creating new Local Date for a year later
LocalDate nextYear = gameRelease.plusYears(1);

//Creating nextYear’s day
int nextDay = nextYear.getDayOfMonth();

//Creating nextYear’s month
int nextMonth = nextYear.getMonthValue();

//Creating nextYear’s year
int yearNext = nextYear.getYear();

//Printing out next Year
System.out.printf(“Next year’s date is: ” + nextMonth + “,” + nextDay + “,” + yearNext + “\n”);
}//End of Main
}//End of DagbaComputers Class

———End of Program—————-

————-Output—————

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: