Getting time in Java requires a date function. Let’s take a look at how to use it.
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!