Home >Java >javaTutorial >How to combine date and time using YearMonth class in Java

How to combine date and time using YearMonth class in Java

王林
王林forward
2023-04-24 22:58:051587browse

How to represent fixed dates such as credit card expiration, the answer lies in YearMonth

Similar to the example of checking recurring events on MonthDay, YearMonth is another combination class used to represent the arrival of credit cards Date, FD expiration date, futures option expiration date, etc. You can also use this class to get the number of days in the current month. The lengthOfMonth() method of the YearMonth instance can return the number of days in the current month, which is very useful when judging whether February has 28 or 29 days.

package com.shxt.demo02;  
import java.time.*;  
public class Demo13 {      
public static void main(String[] args) {          
YearMonth currentYearMonth = YearMonth.now();          
System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());          
YearMonth creditCardExpiry = YearMonth.of(2019, Month.FEBRUARY);          
System.out.printf("Your credit card expires on %s %n", creditCardExpiry);      
}  
}

The above is the detailed content of How to combine date and time using YearMonth class in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete