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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人软件18免费 | 久久综合亚洲色hezyo | 色狠狠婷婷97 | 四虎影视在线看 | 男人机机桶女人 | 亚洲国产果果在线播放在线 | 亚洲 欧美 中文字幕 在线 | 青青草原手机在线视频 | 欧美亚洲国产一区二区三区 | 乳环贵妇堕落开发调教番号 | 欧美成年黄网站色高清视频 | 日本高清视频一区二区 | 羞羞在线观看 | 日本高清在线播放一区二区三区 | 美女脱了内裤打开腿让人羞羞软件 | 啪一啪在线视频 | 亚洲精品tv久久久久久久久久 | 国产成人精品视频午夜 | 国产午夜成人无码免费看 | 五月天婷婷网亚洲综合在线 | 久久精视频 | 天天干天天色综合 | 调教女帝 | 青草香蕉精品视频在线观看 | 日本韩国一区二区三区 | 天天操天天干天天舔 | 爱草视频 | 千金肉奴隶免费观看 | 成人欧美一区二区三区白人 | 欧美在线视频 一区二区 | 成年人免费在线看的惊悚动作片 | 男人的j伸到女人的屁股眼 男人吃奶动态图 | 丝袜美女被艹 | 亚洲AV午夜精品麻豆AV | 日本b站一卡二不卡三卡四卡 | 成人啪啪漫画全文阅读 | 青草视频在线观看免费网站 | 免费看片黄色 | www.久久av.com| 亚洲精品国偷拍自产在线观看蜜臀 | 国产一级特黄在线播放 |