服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java实现简单日期计算功能

java实现简单日期计算功能

2021-06-18 14:02集成显卡 Java教程

这篇文章主要为大家详细介绍了java实现简单日期计算功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文讲的java日期计算比较偏,用到的地方很少(比如获取今天所在周的周一或者周日,获取今天是本月的第几周...),这些方法是以前做项目遗留下来的,现在整理一下,跟大家分享。

工具类主要有一下方法:

public static date getfirstmondayofmonth(string datestring, string dateformat) throws exception
获取指定月份的第一个星期一,比如2014-12 月的第一个周一是2014-12-01

public static int figureweekindexofmonth(string datestring, string dateformat) throws exception
计算指定时间属于月份中的第几周,比如2014-12月的第一周是1号到7号,那么2014-12-05 就是12月的第一周,2014-12-12 就是第二周

public static string getmondyoftoday(string format)
获取今天所在周的星期一, 返回一个时间字符串。 如今天是2014-12-8,那么返回的是: 2014-12-08 (今天刚好是本周周一)

public static date getsundayoftoday()
获取今天所在周的星期天, 如今天是2014-12-8,那么返回的是 2014-12-14

下面是工具类的详细代码:

?
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.calendar;
import java.util.date;
import java.util.list;
 
/**
 * @文件名称 :dateutil.java
 * @所在包 :com.nerve.human.common.util
 * @功能描述 :
 * 时间格式工具类
 * @创建者 :集成显卡 1053214511@qq.com
 * @公司:ibm gdc
 * @创建日期 :2013-4-9
 * @log :
 */
public class dateutil {
 
 public static date todate(string timestring, string format) throws exception{
 return new simpledateformat(format).parse(timestring);
 }
 
 /**
 *
 * @method name: tostring
 * @return type: string
 * @param date
 * @param format
 * @return
 */
 public static string tostring(date date, string format){
 string strtime = null;
 try {
 simpledateformat simpledateformat = new simpledateformat(format);
 strtime = simpledateformat.format(date);
 } catch (exception ex) {
 system.err.println("格式化日期错误 : " + ex.getmessage());
 }
 return strtime;
 }
 /**
 * 获取当月的第一个星期一(以中国为例)
 * @method name: getfirstmonday
 * @return type: void
 */
 public static date getfirstmondayofmonth(string month) throws exception{
 return getfirstmondayofmonth(month, "yyyy-mm");
 }
 
 /**
 * 获取当月的第一个星期一(以中国为例)
 * @method name: getfirstmonday
 * @return type: void
 */
 public static date getfirstmondayofmonth(string datestring, string dateformat) throws exception{
 date date = todate(datestring, dateformat);
 
 calendar c = calendar.getinstance();
 c.settime(date);
 
 int step = (9 - c.get(calendar.day_of_week)) % 7;
 c.add(calendar.day_of_year, step);
 
 return c.gettime();
 }
 
 /**
 * 计算指定时间属于月份中的第几周
 * 比如2014-12月的第一周是1号到7号
 * 那么2014-12-05 就是12月的第一周
 * 2014-12-12 就是第二周
 *
 * @method name: figureweekindexofmonth
 * @return type: int
 *
 * @param date
 * @return
 */
 public static int figureweekindexofmonth(string datestring, string dateformat) throws exception{
 calendar c = calendar.getinstance();
 
 date curdate = todate(datestring, dateformat);
 c.settime(curdate);
 int day = c.get(calendar.day_of_month);
 
 simpledateformat sdf = new simpledateformat("yyyy-mm");
 date firstmondy = getfirstmondayofmonth(sdf.format(c.gettime()));
 c.settime(firstmondy);
 
 int index = 0;
 do{
 c.add(calendar.day_of_month, 7);
 index ++;
 }
 while(c.get(calendar.day_of_month) < day);
 
 return index;
 }
 
 /**
 * 获取今天所在周的星期一
 * @method name: getmondyoftoday
 * @return type: string
 *
 * @return
 */
 public static string getmondyoftoday(string format){
 calendar c = calendar.getinstance();
 int step = c.get(calendar.day_of_week);
 //星期天
 if(step == 1)
 step = 6;
 else
 step -= 2;
 
 c.add(calendar.day_of_year, -step);
 
 return tostring(c.gettime(), format);
 }
 
 /**
 * 获取今天所在周的星期天
 * @method name: getmondyoftoday
 * @return type: string
 *
 * @return
 */
 public static date getsundayoftoday(){
 calendar c = calendar.getinstance();
 
 int step = c.get(calendar.day_of_week);
 if(step != calendar.sunday)
 c.add(calendar.day_of_year, 8-step);
 return c.gettime();
 }
 
 /**
 * 获取指定时间所在的星期天
 * @param date
 * @return
 */
 public static date getsundayofdate(date date){
 calendar c = calendar.getinstance();
 c.settime(date);
 
 int step = c.get(calendar.day_of_week);
 if(step != calendar.sunday)
 c.add(calendar.day_of_year, 8-step);
 return c.gettime();
 }
}

来个测试截图:

java实现简单日期计算功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/ssrc0604hx/article/details/41800623

延伸 · 阅读

精彩推荐