获取某年某月的日期

做财务系统经常会用到会计期间的计算,总结了一些!

    /**
     * 得到某年某月的第一天
     * 
     * @param year
     * @param month
     * @return
     */	    
public static  String getFirstDayOfMonth(Integer year, Integer month) {
	        Calendar cal = Calendar.getInstance();
	        cal.set(Calendar.YEAR, year);
	        cal.set(Calendar.MONTH, month-1);
	        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
	        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	       }

	 /**
     * 返回指定年月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month,-1);
        calendar.roll(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }
<pre name="code" class="java">  <pre name="code" class="java"> /**
     * 返回指定年月的最后一天
     *
     * @param year
     * @param month
     * @return
     */

 
  public static String getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
         
        return lastDayOfMonth;
    }	


 

/** 
	 * 比较两个日期之间的大小 
	 *  
	 * @param d1 
	 * @param d2 
	 * @return 前者大于后者返回true 反之false 
	 */ 
	public static boolean compareDate(Date d1, Date d2) {  
	    Calendar c1 = Calendar.getInstance();  
	    Calendar c2 = Calendar.getInstance();  
	    c1.setTime(d1);  
	    c2.setTime(d2);  
	  
	    int result = c1.compareTo(c2);  
	    if (result >= 0)  
	        return true;  
	    else  
	        return false;  
	}
	
public static String getFirstDate(){ //第一天的日期
		Calendar   ca   =   Calendar.getInstance();
		ca.setTime(new Date());                            //  someDate 为你要获取的那个月的时间
		ca.set(Calendar.DAY_OF_MONTH,   1);
		Date   firstDate   =   ca.getTime();
		return  getDateFormat(firstDate);
		
	}
	public static String getDateFormat(Date date) {
		//Date now = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		return sf.format(date);
	}

	public static String getTimeFormat(Date date) {//带有时分秒的日期格式
		//Date now = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sf.format(date);
	}
	public static Date StringToDate(String str){//字符串转换为日期格式
		if(str==null||str.trim().equals("")) return null;
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			date = sf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			date = null;
		}
		return date;
	}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值