Monday 15 January 2018

How to set day, month and year of current date using with methods of Java 8 LocalDate Class


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

LocalDateDemo.java

import java.time.LocalDate;

public class LocalDateDemo
{

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

        /*
         * Parameters:
         *
         * dayOfMonth - the day-of-month to set in the result, from 1
         * to 28-31
         *
         * Returns:a LocalDate based on this date with the requested
         * day, not null
         */

        LocalDate dateAfterDayChanged = date.withDayOfMonth(30);
        System.out.println("dateAfterDayChanged   = " + dateAfterDayChanged);

        /*
         * Parameters:
         *
         * month - the month-of-year to set in the result, from 1
         * (January) to 12 (December)
         *
         * Returns:a LocalDate based on this date with the requested
         * month, not null
         */

        LocalDate dateAfterMonthChanged = date.withMonth(2);
        System.out.println("dateAfterMonthChanged = " + dateAfterMonthChanged);

        /*
         * Parameters:
         *
         * year - the year to set in the result, from MIN_YEAR to
         * MAX_YEAR
         *
         * Returns:a LocalDate based on this date with the requested
         * year, not null
         */

        LocalDate dateAfterYearChanged = date.withYear(2050);
        System.out.println("dateAfterYearChanged  = " + dateAfterYearChanged);

    }

}

Output

date                  = 2018-01-05
dateAfterDayChanged   = 2018-01-30
dateAfterMonthChanged = 2018-02-05
dateAfterYearChanged  = 2050-01-05

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/f50b0477cba623e7ace08d1d7966409dcc9e7b40/BasicJava/LocalDateDemo_with/?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