java日期格式处理及月份第一天和最后一天

本文介绍了一个Java实用工具类,用于获取月份的第一天和最后一天的日期字符串,并提供了灵活的日期格式化方法。该工具类利用了Java的Calendar和SimpleDateFormat类来实现日期的灵活处理。

 import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class HandleDateUtil {

//日期格式化
 private static SimpleDateFormat simpleFormate = new SimpleDateFormat(" yyyy-MM-dd ");
 
 /**
  *
  * @description: 按日期格式返回月份第一天.
  * @author: liuxmi
  * @Date:Sep 9, 2009
  * @return
  * @return:String
  */
 public static String getFirstDayOfMonth() {
  Calendar calendar = new GregorianCalendar();
  calendar.set(Calendar.DATE, 1);
  System.out.println(simpleFormate.format(calendar.getTime()));
  return simpleFormate.format(calendar.getTime());
 }
 
 /**
  *
  * @description: 按日期格式返回月份最后天.
  * @author: liuxmi
  * @Date:Sep 9, 2009
  * @return
  * @return:String
  */
 public static String getLastDayOfMonth() {
  Calendar calendar = new GregorianCalendar();
  calendar.set(Calendar.DATE, 1);
  calendar.roll(Calendar.DATE, -1);
  System.out.println(simpleFormate.format(calendar.getTime()));
  return simpleFormate.format(calendar.getTime());
 }
/**

* 返回当前格式日期

*/
 public static String  getDateFormat(Date date) {
  return simpleFormate.format(date);
 }
 /**

*返回特定格式的日期

*/
 public static String  getDateFormat(Date date,SimpleDateFormat dateFormat) {
  return dateFormat.format(date);
 }
 /**
  *
  * @description: 返回指定年月的月的第一天.
  * @author: liuxmi
  * @Date:Sep 11, 2009
  * @param year
  * @param month
  * @return
  * @return:String
  */
 public static String getFirstDayOfMonth(Integer year,Integer month){
  Calendar calendar = new GregorianCalendar();
  if(year == null){
   year = calendar.get(Calendar.YEAR);
  }
  if(month == null){
   month = calendar.get(Calendar.MONTH);
  }
  calendar.set(year, month, 1);
  System.out.println(simpleFormate.format(calendar.getTime()));
  return simpleFormate.format(calendar.getTime());
 }
 /**
  *
  * @description: 返回指定年月的月的最后一天.
  * @author: liuxmi
  * @Date:Sep 11, 2009
  * @param year
  * @param month
  * @return
  * @return:String
  */
 public static String getLastDayOfMonth(Integer year,Integer month) {
  Calendar calendar = new GregorianCalendar();
  if(year == null){
   year = calendar.get(Calendar.YEAR);
  }
  if(month == null){
   month = calendar.get(Calendar.MONTH);
  }
  calendar.set(year, month, 1);
  calendar.roll(Calendar.DATE, -1);
  System.out.println(simpleFormate.format(calendar.getTime()));
  return simpleFormate.format(calendar.getTime());
 }
    
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值