Obtaining a Year-Month-Day Date Combination - Java Date Time

Java examples for Date Time:Local Date Time

Solution 1

Use the java.time.YearMonth class.

This class is used to represent the month of a specific year.

YearMonth yearMo = YearMonth.now(); 
System.out.println("Current Year and month:" + yearMo);       
YearMonth specifiedDate = YearMonth.of(2000, Month.NOVEMBER); 
System.out.println("Specified Year-Month: " + specifiedDate); 

Solution 2

Use of the java.time.MonthDay class.

MonthDay monthDay = MonthDay.now(); 
System.out.println("Current month and day: " + monthDay);         

Full Source Code

Demo Code

import java.time.Month;
import java.time.MonthDay;
import java.time.YearMonth;

public class Main {
    public static void main(String[] args) {
        YearMonth yearMo = YearMonth.now();
        System.out.println("Current Year and month:" + yearMo);
        //w  ww. jav a2  s . com
        YearMonth specifiedDate = YearMonth.of(2000, Month.NOVEMBER);
        System.out.println("Specified Year-Month: " + specifiedDate);

        MonthDay monthDay = MonthDay.now();
        System.out.println("Current month and day: " + monthDay);
        
        monthDay = MonthDay.of(Month.NOVEMBER, 11);
        System.out.println("Specified Month-Day: " + specifiedDate);
    }
   
}

Result


Related Tutorials