How to represent fixed date like credit card expiry, YearMonth in java 8?

Java 8Object Oriented ProgrammingProgramming

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay,Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.

Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.

The of() method of java.time.LocalDate class accepts three integer parameters representing and year, a month of an year, day of a month and, returns the instance of the LocalDate object from the given details.

Example

Following Java program reads year, month and day values from the user and creates a Date object of the given date using the classes and methods java.time package of Java8.

import java.time.LocalDate;
import java.util.Scanner;
public class LocalDateJava8 {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the year: ");
      int year = sc.nextInt();
      System.out.println("Enter the month: ");
      int month = sc.nextInt();
      System.out.println("Enter the day: ");
      int day = sc.nextInt();
      //Getting the current date value
      LocalDate givenDate = LocalDate.of(year, month, day);
      System.out.println("Date: "+givenDate);
   }
}

Output

Enter the year:
2019
Enter the month:
07
Enter the day:
24
Date: 2019-07-24
raja
Published on 07-Aug-2019 14:52:07
Advertisements