This article describes the method of Java implementation to obtain the first/last day of a certain month of a certain year. Share it for your reference, as follows:
Java obtains the first day of a certain month
Design source code
FisrtDayOfMonth.java:
/** * @Title:FisrtDayOfMonth.java * @Package:com.you.freemarker.model * @Description: Get the first day of a certain month of a certain year* @author:Youhaidong (Youhaidong) * @version V1.0 */package com.you.freemarker.model;import java.text.SimpleDateFormat;import java.util.Calendar;/** * Class function description* Class modifier modification date* Modification description* <p>Title:FisrtDayOfMonth.java</p> * <p>Description:Youhaidong Personal Development</p> * <p>Copyright:Copyright(c)2013</p> * @author:You Haidong* @version V1.0 */public class FisrtDayOfMonth{ /** * Get the first day of a certain month of a certain year* @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(); //Set year cal.set(Calendar.YEAR, year); //Set month cal.set(Calendar.MONTH, month-1); //Get the minimum number of days for a certain month int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //Set the minimum number of days for the month in the calendar cal.set(Calendar.DAY_OF_MONTH, firstDay); //Format date 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("Wulin.com test result: "); System.out.println("Get the first day of the current month: " + firstDay); }}Running results
Java gets the last day of a certain month
Design source code
LastDayOfMonth.java:
/** * @Title:LastDayOfMonth.java * @Package:com.you.freemarker.model * @Description: Get the last day of a certain month* @author:Youhaidong (Youhaidong) * @date:2014-5-29 10:58:20 pm * @version V1.0 */package com.you.freemarker.model;import java.text.SimpleDateFormat;import java.util.Calendar;/** * Class function description* Class modifier modification date* Modification description* <p>Title:LastDayOfMonth.java</p> * <p>Description:You Haidong Personal Development</p> * <p>Copyright:Copyright(c)2013</p> * @author:You Haidong* @version V1.0 */public class LastDayOfMonth{ /** * Get the last day of a certain month* @Title:getLastDayOfMonth * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getLastDayOfMonth(int year,int month) { Calendar cal = Calendar.getInstance(); //Set year cal.set(Calendar.YEAR, year); //Set month cal.set(Calendar.MONTH, month-1); //Get the maximum number of days in a certain month int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //Set the maximum number of days of the month in the calendar cal.set(Calendar.DAY_OF_MONTH, lastDay); //Format date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String lastDayOfMonth = sdf.format(cal.getTime()); return lastDayOfMonth; } /** * @Title:main * @Description: * @param:@param args * @return: void * @throws */ public static void main(String[] args) { String lastDay = getLastDayOfMonth(2014,5); System.out.println("Wulin.com test result: "); System.out.println("Get the last day of the current month: " + lastDay); }}Running results
PS: Here are a few online tools for your reference:
Online Date/Day Calculator:
http://tools.VeVB.COM/jisuanqi/date_jisuanqi
Online Perpetual Calendar:
http://tools.VeVB.COM/bianmin/wannianli
Online Lunar/Gregorian calendar conversion tool:
http://tools.VeVB.COM/bianmin/yinli2yangli
Unix timestamp conversion tool:
http://tools.VeVB.COM/code/unixtime
For more information about Java related content, please check out the topics of this site: "Summary of Java Date and Time Operation Skills", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.