本文實例講述了java實現獲取某年某月第一天/最后一天的方法。分享給大家供大家參考,具體如下:
java獲取某年某月的第一天
設計源碼
fisrtdayofmonth.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/** * @title:fisrtdayofmonth.java * @package:com.you.freemarker.model * @description:獲取某年某月的第一天 * @author:youhaidong(游海東) * @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:游海東 * @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( "服務器之家測試結果:" ); system.out.println( "獲取當前月的第一天:" + firstday); } } |
運行結果
java獲取某年某月的最后一天
設計源碼
lastdayofmonth.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
/** * @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:游海東 * @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( "服務器之家測試結果:" ); system.out.println( "獲取當前月的最后一天:" + lastday); } } |
運行結果
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/you23hai45/article/details/27588553