Date Classes IN JAVA 8



Date Classes

The Date-Time API provides four classes that deal with date information, without respect to time or time zone.

These classes are

  • LocalDate
  • YearMonth
  • MonthDay
  • Year

LocalDate

A LocalDate class can represent date i.e. year-month-day without time.

Birth date or wedding date can be easily tracked using LocalDate class. The following examples use the of and with methods to create instances of LocalDate

LocalDate date = LocalDate.of(2005, Month.DECEMBER, 12);

LocalDate nextWed = date.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));

getDayOfWeek method. This method find the day of the week that a particular date falls on.

DayOfWeek dotw = LocalDate.of(2014, Month.NOVEMBER, 17).getDayOfWeek()
will return MONDAY

Example to retrieve the first SUNADY after a specific date.

class DateTimeDemo
{
public static void main(String[] args)
{
LocalDate date = LocalDate.of(2014, Month.NOVEMBER, 17);
TemporalAdjuster adj = TemporalAdjusters.next(DayOfWeek.SUNDAY);
LocalDate nextWed = date.with(adj);
System.out.printf("For the date of %s, the next SUNDAY is %s.%n",date, nextWed);
}
}

For the date of 2014-11-17, the next SUNDAY is 2014-11-23.


YearMonth

The YearMonth class represents the month of a specific year. The following example uses the YearMonth.lengthOfMonth() method to determine the number of days for several year and month combinations.

Example Of YearMonth


class YearMonthDemo
{
public static void main(String[] args)
{
YearMonth date = YearMonth.now();
System.out.printf("%s: %d%n", date, date.lengthOfMonth());

YearMonth date2 = YearMonth.of(2014, Month.FEBRUARY);
System.out.printf("%s: %d%n", date2, date2.lengthOfMonth());

YearMonth date3 = YearMonth.of(2012, Month.FEBRUARY);
System.out.printf("%s: %d%n", date3, date3.lengthOfMonth());
}
}


Output Is

2014-11: 30
2014-02: 28
2012-02: 29

Display the numnber of days in each month of the specified year.


import java.time.Month;
import java.time.Year;
import java.time.YearMonth;
import java.time.DateTimeException;

import java.io.PrintStream;
import java.lang.NumberFormatException;

public class MonthsInYear {
    public static void main(String[] args) {
        int year = 0;

        if (args.length <= 0) {
            System.out.printf("Usage: MonthsInYear %n");
            throw new IllegalArgumentException();
        }

        try {
            year = Integer.parseInt(args[0]);
        } catch (NumberFormatException nexc) {
            System.out.printf("%s is not a properly formatted number.%n",
                args[0]);
            throw nexc;     // Rethrow the exception.
        }

        try {
            Year test = Year.of(year);
        } catch (DateTimeException exc) {
            System.out.printf("%d is not a valid year.%n", year);
            throw exc;     // Rethrow the exception.
        }

        System.out.printf("For the year %d:%n", year);
        for (Month month : Month.values()) {
            YearMonth ym = YearMonth.of(year, month);
            System.out.printf("%s: %d days%n", month, ym.lengthOfMonth());
        }
    }
}



MonthDay

As the name suggest MonthDay class represents the day of a particular month, such as New Year's Day on January 1.

The following example uses the MonthDay.isValidYear method to determine if February 29 is valid for the year 2014. The call returns false, confirming that 2014 is not a leap year.

MonthDay date = MonthDay.of(Month.FEBRUARY, 29);

boolean validLeapYear = date.isValidYear(2014);

Year

The Year class represents a year.

The following example uses the Year.isLeap method to determine if the given year is a leap year. The call returns true, confirming that 2014is a leap year or not.

boolean validLeapYear = Year.of(2014).isLeap();




Display all of the Mondays in the current year and the specified month.

import java.time.Month;
import java.time.Year;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.DateTimeException;

import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import java.io.PrintStream;
import java.lang.NumberFormatException;

public class ListMondays {
    public static void main(String[] args) {
        Month month = null;

        if (args.length < 1) {
            System.out.printf("Usage: ListMondays %n");
            throw new IllegalArgumentException();
        }

        try {
            month = Month.valueOf(args[0].toUpperCase());
        } catch (IllegalArgumentException exc) {
            System.out.printf("%s is not a valid month.%n", args[0]);
            throw exc;      // Rethrow the exception.
        }

        System.out.printf("For the month of %s:%n", month);
        LocalDate date = Year.now().atMonth(month).atDay(1).
              with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        Month mi = date.getMonth();
        while (mi == month) {
            System.out.printf("%s%n", date);
            date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
            mi = date.getMonth();
        }
    }
}