Calendar 日期处理 入门简单使用

//更详细见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);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值