CodesJava

Easy learning with example program codes

Java 8 MonthDay class


The java.time.MonthDay is an immutable date-time class that represents the combination of a month and day-of-month. Any field that can be derived from a month and day, such as quarter-of-year, can be obtained.

Java MonthDay class methods

Method Description
LocalDate atYear(int year) It is used to combine this month-day with a year to create a LocalDate.
String format(DateTimeFormatter formatter) It is used to format this month-day using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this month-day as an int.
boolean isValidYear(int year) It is used to check if the year is valid for this month-day.
static MonthDay now() It is used to obtain the current month-day from the system clock in the default time-zone.
static MonthDay of(int month, int dayOfMonth) It is used to obtain an instance of MonthDay.
ValueRange range(TemporalField field) It is used to get the range of valid values for the specified field.

Example

package com.codesjava;
 
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
 
public class TestExample {
	public static void main(String args[]){
	    MonthDay month = MonthDay.now();  
	    LocalDate date = month.atYear(1998);  
	    System.out.println(date);  
 
	    //Get month
	    long n = month.get(ChronoField.MONTH_OF_YEAR);  
	    System.out.println(n);  
 
	    //Get range
	    ValueRange r1 = month.range(ChronoField.MONTH_OF_YEAR);  
	    System.out.println(r1);  
	    ValueRange r2 = month.range(ChronoField.DAY_OF_MONTH);  
	    System.out.println(r2);  
	}  
}

Output

1998-04-15
4
1 - 12
1 - 30
Posted in Date   


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap