一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - 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
 * @功能描述 :
 * 時間格式工具類
 * @創建者 :集成顯卡 [email protected]
 * @公司: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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青苹果乐园影院免费观看完整版 | 欧美视频在线播放观看免费福利资源 | 美女张开大腿让男人桶 | 9l桃色| 极品丝袜小说全集 | 九九热只有精品 | 99久9在线视频 | 毛片一区二区三区提莫影院 | china精品对白普通话 | 97精品久久天干天天蜜 | 精品久久久久久久国产潘金莲 | 青山葵在线 | 亚洲春色综合另类网蜜桃 | 国产亚洲精品网站 | 精品国产欧美一区二区 | 男人操女人动图 | 日本福利视频一区 | 色播影院性播影院私人影院 | 免费亚洲一区 | 热99在线视频 | 猛操美女 | 男人把大ji巴放进女人小说 | youjizzxxx在线观看 | 久久免费看少妇级毛片蜜臀 | 欧美日韩在线观看精品 | 91东航翘臀女神在线播放 | 国产老熟 | 91成| 小寡妇好紧进去了好大看视频 | 亚洲www视频 | 亚洲国产精品综合一区在线 | 色播影院性播影院私人影院 | 亚洲成人国产精品 | 欧美日韩国产成人综合在线 | 精品网站| 亚洲视频在线观看地址 | 四虎最新永久免费网址 | 日本福利片国产午夜久久 | 亚洲视频一区二区在线观看 | jm漫天堂破解版 | 日韩伦理一区 |