Java实现获取某年某月第一天/最后一天的方法

2025-05-27 0 87

本文实例讲述了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("快网idc测试结果:");

system.out.println("获取当前月的第一天:" + firstday);

}

}

运行结果

Java实现获取某年某月第一天/最后一天的方法

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("快网idc测试结果:");

system.out.println("获取当前月的最后一天:" + lastday);

}

}

运行结果

Java实现获取某年某月第一天/最后一天的方法

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/you23hai45/article/details/27588553

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java实现获取某年某月第一天/最后一天的方法 https://www.kuaiidc.com/76503.html

相关文章

发表评论
暂无评论