zoukankan      html  css  js  c++  java
  • Java获取某年某月的最后一天

    Java获取某年某月的最后一天


    1、设计源码

    LastDayOfMonth.java:

    /**
     * @Title:LastDayOfMonth.java
     * @Package:com.you.freemarker.model
     * @Description:获取某月的最后一天
     * @author:Youhaidong(游海东)
     * @date:2014-5-29 下午10:58:20
     * @version V1.0
     */
    package com.you.freemarker.model;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    /**
     * 类功能说明
     * 类修改者 修改日期
     * 修改说明
     * <p>Title:LastDayOfMonth.java</p>
     * <p>Description:游海东个人开发</p>
     * <p>Copyright:Copyright(c)2013</p>
     * @author:游海东
     * @date:2014-5-29 下午10:58:20
     * @version V1.0
     */
    public class LastDayOfMonth 
    {
    	/**
    	 * 获取某月的最后一天
    	 * @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();
    		//设置年份
    		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;
    	}
    
    	/**
    	 * @Title:main
    	 * @Description:
    	 * @param:@param args
    	 * @return: void
    	 * @throws
    	 */
    	public static void main(String[] args) 
    	{
            String lastDay = getLastDayOfMonth(2014,5);
            System.out.println("获取当前月的最后一天:" + lastDay);
    	}
    
    }
    

    2、运行结果

    获取当前月的最后一天:2014-05-31


  • 相关阅读:
    LeetCode 842. Split Array into Fibonacci Sequence
    LeetCode 1087. Brace Expansion
    LeetCode 1219. Path with Maximum Gold
    LeetCode 1079. Letter Tile Possibilities
    LeetCode 1049. Last Stone Weight II
    LeetCode 1046. Last Stone Weight
    LeetCode 1139. Largest 1-Bordered Square
    LeetCode 764. Largest Plus Sign
    LeetCode 1105. Filling Bookcase Shelves
    LeetCode 1027. Longest Arithmetic Sequence
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13315075.html
Copyright © 2011-2022 走看看