Home > Java > javaTutorial > How to get the current month in java

How to get the current month in java

(*-*)浩
Release: 2019-05-21 14:46:30
Original
6951 people have browsed it

Getting time in Java requires a date function. Let’s take a look at how to use it.

How to get the current month in java

First use the calendar class and then use its methods to get the current month

package test;
import java.util.Calendar;
public class Test {
    public static void main(String[] args) {
        Calendar cal=Calendar.getInstance();//使用日历类
        int month=cal.get(Calendar.MONTH)+1;//得到月,因为从0开始的,所以要加1
        System.out.println("当前月份:"+month);
    }
}
Copy after login

Result:

当前月份:5
Copy after login

The above is the detailed content of How to get the current month in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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