国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java實(shí)現(xiàn)簡(jiǎn)單日期計(jì)算功能

java實(shí)現(xiàn)簡(jiǎn)單日期計(jì)算功能

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

這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單日期計(jì)算功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文講的java日期計(jì)算比較偏,用到的地方很少(比如獲取今天所在周的周一或者周日,獲取今天是本月的第幾周...),這些方法是以前做項(xiàng)目遺留下來(lái)的,現(xiàn)在整理一下,跟大家分享。

工具類主要有一下方法:

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

public static int figureweekindexofmonth(string datestring, string dateformat) throws exception
計(jì)算指定時(shí)間屬于月份中的第幾周,比如2014-12月的第一周是1號(hào)到7號(hào),那么2014-12-05 就是12月的第一周,2014-12-12 就是第二周

public static string getmondyoftoday(string format)
獲取今天所在周的星期一, 返回一個(gè)時(shí)間字符串。 如今天是2014-12-8,那么返回的是: 2014-12-08 (今天剛好是本周周一)

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

下面是工具類的詳細(xì)代碼:

?
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
 * @功能描述 :
 * 時(shí)間格式工具類
 * @創(chuàng)建者 :集成顯卡 1053214511@qq.com
 * @公司:ibm gdc
 * @創(chuàng)建日期 :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("格式化日期錯(cuò)誤 : " + ex.getmessage());
 }
 return strtime;
 }
 /**
 * 獲取當(dāng)月的第一個(gè)星期一(以中國(guó)為例)
 * @method name: getfirstmonday
 * @return type: void
 */
 public static date getfirstmondayofmonth(string month) throws exception{
 return getfirstmondayofmonth(month, "yyyy-mm");
 }
 
 /**
 * 獲取當(dāng)月的第一個(gè)星期一(以中國(guó)為例)
 * @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();
 }
 
 /**
 * 計(jì)算指定時(shí)間屬于月份中的第幾周
 * 比如2014-12月的第一周是1號(hào)到7號(hào)
 * 那么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();
 }
 
 /**
 * 獲取指定時(shí)間所在的星期天
 * @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();
 }
}

來(lái)個(gè)測(cè)試截圖:

java實(shí)現(xiàn)簡(jiǎn)單日期計(jì)算功能

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久九| 亚洲成人免费在线 | 午夜免费 | 精品国产一区二区在线 | 亚洲一视频 | 国产资源大全 | 完全免费av | 国产一区二区精品 | 黄久久久 | 欧美日韩在线播放 | 黄色国产一级片 | 成人免费看黄 | 午夜黄色影院 | 国产成人精品一区二区三区四区 | 中文字幕av在线播放 | 午夜成人在线视频 | 亚洲三级电影 | 中文在线一区二区 | 久久久www | 日本久久精品视频 | 国产片在线免费播放 | 亚洲影视在线 | 久久夜夜| 亚洲激情 欧美 | 国产成人高清 | 久久久久久久久久久美女 | 欧美精品欧美极品欧美激情 | 国产一区二区三区免费在线 | 亚洲国产传媒99综合 | 久久99精品久久久久久水蜜桃 | 伊人伊人网 | 色视频www在线播放国产人成 | 精品国产一区二区三区av性色 | 一区二区国产精品 | 免费在线观看黄色av | 亚洲日韩欧美一区二区在线 | 综合五月| 欧美日韩网站 | 综合久久综合久久 | 999精品一区 | 中文字幕成人av |