Java implementation to obtain the method of the First Day/last day of a month in a year, the last day of java
This example describes how to obtain the first day/last day of a month in Java. We will share this with you for your reference. The details are as follows:
Java obtains the first day of a month.
Design source code
FisrtDayOfMonth. java:
/*** @ Title: FisrtDayOfMonth. java * @ Package: com. you. freemarker. model * @ Description: Get the first day of a month of a 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: youhaidong * @ version V1.0 */public class FisrtDayOfMonth {/*** get the first day of a month of a 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 the year cal. set (Calendar. YEAR, year); // sets the month cal. set (Calendar. MONTH, month-1); // obtain the minimum number of days of a MONTH int firstDay = cal. getActualMinimum (Calendar. DAY_OF_MONTH); // sets the minimum number of days of the month in the calendar cal. set (Calendar. DAY_OF_MONTH, firstDay); // format the 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 (hz5); System. out. println ("helper house Test Result:"); System. out. println ("first day of the current month:" + firstDay );}}
Running result
Java obtains the last day of a month.
Design source code
LastDayOfMonth. java:
/*** @ Title: LastDayOfMonth. java * @ Package: com. you. freemarker. model * @ Description: Get the last day of a month * @ author: Youhaidong (Youhaidong) * @ date: 10:58:20 * @ 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: youhaidong personal development </p> * <p> Copyright: Copyright (c) 2013 </p> * @ author: youhaidong * @ version V1.0 */public class LastDayOfMonth {/*** get the last day of a 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 the year cal. set (Calendar. YEAR, year); // sets the month cal. set (Calendar. MONTH, month-1); // obtain the maximum number of days of a MONTH int lastDay = cal. getActualMaximum (Calendar. DAY_OF_MONTH); // sets the maximum number of days of the month in the calendar cal. set (Calendar. DAY_OF_MONTH, lastDay); // format the 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 (hz5); System. out. println ("helper house Test Result:"); System. out. println ("Get the last day of the current month:" + lastDay );}}
Running result
PS: Here are some online tools for date and time computing for your reference:
Online date/day calculator:
Http://tools.jb51.net/jisuanqi/date_jisuanqi
Online calendar:
Http://tools.jb51.net/bianmin/wannianli
Online calendar/Calendar Conversion Tool:
Http://tools.jb51.net/bianmin/yinli2yangli
Unix timestamp Conversion Tool:
Http://tools.jb51.net/code/unixtime