Java获取某年某月的第一天
1、设计源码
FisrtDayOfMonth.java:
/** * @Title:FisrtDayOfMonth.java * @Package:com.you.freemarker.model * @Description:获取某年某月的第一天 * @author:Youhaidong(游海东) * @date:2014-5-29 下午11:21:31 * @version V1.0 */ package com.you.freemarker.model; import java.text.SimpleDateFormat; import java.util.Calendar; /** * 类功能说明 * 类修改者 修改日期 * 修改说明 * <p>Title:FisrtDayOfMonth.java</p> * <p>Description:游海东个人开发</p> * <p>Copyright:Copyright(c)2013</p> * @author:游海东 * @date:2014-5-29 下午11:21:31 * @version V1.0 */ public class FisrtDayOfMonth { /** * 获取某年某月的第一天 * @Title:getFisrtDayOfMonth * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getFisrtDayOfMonth(int year,int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String firstDayOfMonth = sdf.format(cal.getTime()); return firstDayOfMonth; } /** * @Title:main * @Description: * @param:@param args * @return: void * @throws */ public static void main(String[] args) { String firstDay = getFisrtDayOfMonth(2014,5); System.out.println("获取当前月的最后一天:" + firstDay); } }
2、运行结果
获取当前月的最后一天:2014-05-01