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

How to get the current month in java

(*-*)浩
(*-*)浩Original
2019-05-21 10:53:136880browse

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);
    }
}

Result:

当前月份:5

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!

Statement:
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