//更详细见API
public class CalendarDemo {
/**
* 计算某月份最大天数
*/
public static int getMaxDays(Date date) {
Calendar cal = Calendar.getInstance();
cal.clear();// 在使用set方法之前,必须先clear一下,否则很多信息会继承自系统当前时间
cal.setTime(date);
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* Calendar和Date的转化
* @param cal
* @return
*/
public static Date getDate(Calendar cal) {
return cal.getTime();
}
public static void getCalendar(Date date){
Calendar.getInstance().setTime(date);
}
/**
* 格式化输出日期时间
* @param format
* @param date
* @return
*/
public static String getFormatDate(String format,Date date){
SimpleDateFormat sdf = new SimpleDateFormat("formt");
return sdf.format(date).toString();
}
/**
* 计算某一天是一年中的第几星期
* @param date
* @return
*/
public static int getWeekNo(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.WEEK_OF_YEAR);
}
/**
* 年份
* @param date
* @return
*/
public static int getYear(Date date){
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.YEAR);
}
/**
* 月份
* @param date
* @return
*/
public static int getMonth(Date date){
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.MONTH)+1;//Calendar对象默认一月为0
}
/**
* 多少号
* @param date
* @return
*/
public static int getDay(Date date){
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.DAY_OF_YEAR);
}
}