要使用Calendar类,请导入以下包。
import java.util.Calendar;
使用Calendar类,创建一个对象。
Calendar calendar = Calendar.getInstance();
现在,创建一个包含月份名称的字符串数组。
String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December" };
显示月份名称。
month[calendar.get(Calendar.MONTH)]
以下是一个示例。
import java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December" }; System.out.println("Current Month = " + month[calendar.get(Calendar.MONTH)]); } }
输出结果
以下是输出-
Current Month = Dec