Sunday 14 January 2018

How to add weeks and minus weeks from the current date | Java 8 LocalDate Class


Click here to watch on Youtube :
https://www.youtube.com/watch?v=7AHygqbmTLg&list=UUhwKlOVR041tngjerWxVccw

LocalDateDemo1.java

import java.time.LocalDate;

public class LocalDateDemo1
{

    public static void main(String[] args)
    {
        LocalDate currentDate = LocalDate.now();
        System.out.println("currentDate = " + currentDate);

        /*
         * Parameters:
         *
         * weeksToSubtract - the weeks to subtract, may be negative
         *
         * Returns:a LocalDate based on this date with the weeks
         * subtracted, not null
         */

        LocalDate dateAfterWeekChanged = currentDate.minusWeeks(2);
        System.out.println("dateAfterWeekChanged = " + dateAfterWeekChanged);
    }

}

Output

currentDate = 2018-01-05
dateAfterWeekChanged = 2017-12-22

LocalDateDemo2.java

import java.time.LocalDate;

public class LocalDateDemo2
{

    public static void main(String[] args)
    {
        /*
         * Obtains an instance of LocalDate from a text string such as
         * 2007-12-03
         */

        LocalDate currentDate = LocalDate.now();
        System.out.println("currentDate = " + currentDate);

        /*
         * Parameters:
         *
         * weeksToAdd - the weeks to add, may be negative
         *
         * Returns:a LocalDate based on this date with the weeks
         * added, not null
         */

        LocalDate dateAfterWeekChanged = currentDate.plusWeeks(3);
        System.out.println("dateAfterWeekChanged = " + dateAfterWeekChanged);
    }

}

Output

currentDate = 2018-01-05
dateAfterWeekChanged = 2018-01-26

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LocalDateDemo_minus_plus_weeks.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/LocalDateDemo_minus_plus_weeks

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/f50b0477cba623e7ace08d1d7966409dcc9e7b40/BasicJava/LocalDateDemo_minus_plus_weeks/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment