Javaはある年のある月の初日を取得します
1774 ワード
Javaはある年のある月の初日を取得します
1、設計ソースコード
FisrtDayOfMonth.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