java Calendar的常用方式

import java.util.Calendar;
Calendar calendar = new GregorianCalendar();

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());//当前时间

int year = c.get(Calendar.YEAR);    //获取年
int month = c.get(Calendar.MONTH) + 1;   //获取月份,0表示1月份
int day = c.get(Calendar.DAY_OF_MONTH);    //获取当前天数
int first = c.getActualMinimum(c.DAY_OF_MONTH);    //获取本月最小天数
int last = c.getActualMaximum(c.DAY_OF_MONTH);    //获取本月最大天数
int time = c.get(Calendar.HOUR_OF_DAY);       //获取当前小时
int min = c.get(Calendar.MINUTE);          //获取当前分钟
int xx = c.get(Calendar.SECOND);          //获取当前秒


SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String curDate = s.format(c.getTime());  //当前日期
System.out.println("当前时间:"+year + "-" + month + "-"+ day + " "+time + ":" + min +":" + xx);
System.out.println("第一天和最后天:" + first +"," + last);
System.out.println("当前日期curDate:" + curDate);


c.add(Calendar.YEAR, 1); //增加一年
c.add(Calendar.MONTH, 1);//增加一个月
c.add(Calendar.DAY_OF_MONTH, 1);//增加一天
int year2 = c.get(Calendar.YEAR);
int month2 = c.get(Calendar.MONTH) + 1;
int day2 = c.get(Calendar.DAY_OF_MONTH);
int firstD = c.getActualMinimum(c.DAY_OF_MONTH);
int lastD = c.getActualMaximum(c.DAY_OF_MONTH);
System.out.println("当前时间:"+year2 + "-" + month2 + "-"+ day2 + " "+time + ":" + min +":" + xx);
System.out.println("第一天和最后天:" + firstD +"," + lastD);

你可能感兴趣的:(Calendar,Calendar,Java)