Home > Java > javaTutorial > body text

How to combine date and time using YearMonth class in Java

王林
Release: 2023-04-24 22:58:05
forward
1547 people have browsed it

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);      
}  
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template