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

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

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

服務器之家 - 編程語言 - Java教程 - Java日期處理工具類DateUtils詳解

Java日期處理工具類DateUtils詳解

2021-03-01 14:19一汪清水 Java教程

這篇文章主要為大家詳細介紹了Java日期處理工具類DateUtils的相關代碼,包含日期和時間常用操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java日期處理工具類DateUtils的具體代碼,供大家參考,具體內容如下

?
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
/**
 * <日期時間處理工具類>
 */
public class DateUtils {
 
 /**
 * Date format pattern this is often used.
 */
 public static final String PATTERN_YMD = "yyyy-MM-dd";
 
 /**
 * Date format pattern this is often used.
 */
 public static final String PATTERN_YMDHMS="yyyy-MM-dd HH:mm:ss";
 
 /**
 * Formats the given date according to the YMD pattern.
 *
 * @param date The date to format.
 * @return An YMD formatted date string.
 *
 * @see #PATTERN_YMD
 */
 public static String formatDate(Date date) {
 return formatDate(date, PATTERN_YMD);
 }
 
 /**
 * Formats the given date according to the specified pattern. The pattern
 * must conform to that used by the {@link SimpleDateFormat simple date
 * format} class.
 *
 * @param date The date to format.
 * @param pattern The pattern to use for formatting the date.
 * @return A formatted date string.
 *
 * @throws IllegalArgumentException If the given date pattern is invalid.
 *
 * @see SimpleDateFormat
 */
 public static String formatDate(Date date, String pattern) {
 if (date == null)
  throw new IllegalArgumentException("date is null");
 if (pattern == null)
  throw new IllegalArgumentException("pattern is null");
  
 SimpleDateFormat formatter = new SimpleDateFormat(pattern);
 return formatter.format(date);
 }
 
 /**
 * Parses a date value. The format used for parsing the date value are retrieved from
 * the default PATTERN_YMD.
 *
 * @param dateValue the date value to parse
 *
 * @return the parsed date
 *
 * @throws IllegalArgumentException If the given dateValue is invalid.
 */
 public static Date parseDate(String dateValue) {
 return parseDate(dateValue, null);
 }
 
 /**
 * Parses the date value using the given date format.
 *
 * @param dateValue the date value to parse
 * @param dateFormat the date format to use
 *
 * @return the parsed date. if parse is failed , return null
 *
 * @throws IllegalArgumentException If the given dateValue is invalid.
 */
 public static Date parseDate(String dateValue, String dateFormat) {
 if (dateValue == null) {
  throw new IllegalArgumentException("dateValue is null");
 }
 if (dateFormat == null) {
  dateFormat = PATTERN_YMD;
 }
  
 SimpleDateFormat df = new SimpleDateFormat(dateFormat);
 Date result = null;
 try {
  result = df.parse(dateValue);
 }
 catch (ParseException pe) {
  pe.printStackTrace();// 日期型字符串格式錯誤
 }
 return result;
 }
 
 /**
 * Adds a number of years to a date returning a new object.
 * The original date object is unchanged.
 *
 * @param date the date, not null
 * @param amount the amount to add, may be negative
 * @return the new date object with the amount added
 * @throws IllegalArgumentException if the date is null
 */
 public static Date addYears(Date date, int amount) {
 return add(date, Calendar.YEAR, amount);
 }
 
 /**
 * Adds a number of years to a timestamp returning a new object.
 * The original timestamp object is unchanged.
 *
 * @param timestamp the timestamp, not null
 * @param amount the amount to add, may be negative
 * @return the new timestamp object with the amount added
 * @throws IllegalArgumentException if the timestamp is null
 */
 public static Timestamp addYears(Timestamp timestamp, int amount) {
 return add(timestamp, Calendar.YEAR, amount);
 }
 
 //-----------------------------------------------------------------------
 /**
 * Adds a number of months to a date returning a new object.
 * The original date object is unchanged.
 *
 * @param date the date, not null
 * @param amount the amount to add, may be negative
 * @return the new date object with the amount added
 * @throws IllegalArgumentException if the date is null
 */
 public static Date addMonths(Date date, int amount) {
 return add(date, Calendar.MONTH, amount);
 }
 
 /**
 * Adds a number of months to a timestamp returning a new object.
 * The original timestamp object is unchanged.
 *
 * @param timestamp the timestamp, not null
 * @param amount the amount to add, may be negative
 * @return the new timestamp object with the amount added
 * @throws IllegalArgumentException if the timestamp is null
 */
 public static Timestamp addMonths(Timestamp timestamp, int amount) {
 return add(timestamp, Calendar.MONTH, amount);
 }
 
 //-----------------------------------------------------------------------
 /**
 * Adds a number of days to a date returning a new object.
 * The original date object is unchanged.
 *
 * @param date the date, not null
 * @param amount the amount to add, may be negative
 * @return the new date object with the amount added
 * @throws IllegalArgumentException if the date is null
 */
 public static Date addDays(Date date, int amount) {
 return add(date, Calendar.DATE, amount);
 }
 
 /**
 * Adds a number of days to a timestamp returning a new object.
 * The original timestamp object is unchanged.
 *
 * @param timestamp the timestamp, not null
 * @param amount the amount to add, may be negative
 * @return the new timestamp object with the amount added
 * @throws IllegalArgumentException if the timestamp is null
 */
 public static Timestamp addDays(Timestamp timestamp, int amount) {
 return add(timestamp, Calendar.DATE, amount);
 }
 
 //-----------------------------------------------------------------------
 /**
 * Adds a number of minutes to a timestamp returning a new object.
 * The original timestamp object is unchanged.
 *
 * @param timestamp the timestamp, not null
 * @param amount the amount to add, may be negative
 * @return the new timestamp object with the amount added
 * @throws IllegalArgumentException if the timestamp is null
 */
 public static Timestamp addMinutes(Timestamp timestamp, int amount) {
 return add(timestamp, Calendar.MINUTE, amount);
 }
 
 /**
 * Adds a number of days to current time returning a new object.
 *
 * @param amount the amount to add, may be negative
 * @return the new timestamp object with the amount added
 */
 public static Timestamp addDays(int amount) {
 Calendar c = Calendar.getInstance();
 c.add(Calendar.DATE, amount);
 return new Timestamp(c.getTimeInMillis());
 }
 
 //-----------------------------------------------------------------------
 /**
 * Adds to a date returning a new object.
 * The original date object is unchanged.
 *
 * @param date the date, not null
 * @param calendarField the calendar field to add to
 * @param amount the amount to add, may be negative
 * @return the new date object with the amount added
 * @throws IllegalArgumentException if the date is null
 */
 private static Date add(Date date, int calendarField, int amount) {
 if (date == null) {
  throw new IllegalArgumentException("The date must not be null");
 }
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 c.add(calendarField, amount);
 return c.getTime();
 }
 
 /**
 * Adds to a timestamp returning a new object.
 * The original timestamp object is unchanged.
 *
 * @param timestamp the timestamp, not null
 * @param calendarField the calendar field to add to
 * @param amount the amount to add, may be negative
 * @return the new timestamp object with the amount added
 * @throws IllegalArgumentException if the timestamp is null
 */
 private static Timestamp add(Timestamp timestamp, int calendarField, int amount) {
 if (timestamp == null) {
  throw new IllegalArgumentException("The timestamp must not be null");
 }
 Calendar c = Calendar.getInstance();
 c.setTime(timestamp);
 c.add(calendarField, amount);
 return new Timestamp(c.getTimeInMillis());
 }
 
 /**
 * <生成最小的當天日期值>
 * @return 最小的當天日期值
 */
 public static Timestamp now() {
 Calendar c = Calendar.getInstance();
 c.set(Calendar.HOUR_OF_DAY, 0);
 c.set(Calendar.MINUTE, 0);
 c.set(Calendar.SECOND, 0);
 c.set(Calendar.MILLISECOND, 0);
 return new Timestamp(c.getTimeInMillis());
 }
 
 
 
 /** This class should not be instantiated. */
 private DateUtils() {
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/tang9140/article/details/38760013

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 2022国产麻豆剧果冻传媒入口 | 美女口述又粗又大感觉 | 国产日韩欧美综合一区二区三区 | www毛片| 精品国产品国语在线不卡丶 | 国产亚洲人成网站在线观看不卡 | 国产成人精品福利色多多 | 国产精品色片 | 亚洲国产日韩成人综合天堂 | 亚洲男1069gay男猛男 | 美女吃男生鸡鸡 | 免费人成黄页在线观看69 | 人与动人物aaaa | 暖暖视频高清图片免费完整版 | 91精品综合国产在线观看 | 欧美精品一区二区在线观看 | 茄子视频懂你更多apl | 丰满岳乱妇在线观看视频国产 | 国产码一区二区三区 | 男人女人日皮视频 | 久久久久国产一级毛片高清片 | 精品久久久久久久高清 | 四川一级毛片 | 日本在线视频网 | 国内精品自产拍在线观看91 | 亚洲视频免 | 996热视频 | 亚洲社区在线观看 | 久久99精国产一区二区三区四区 | 亚洲国产日韩欧美mv | 午夜在线观看免费完整直播网页 | 99在线播放视频 | 亚洲高清一区二区三区久久 | 日韩天堂在线 | 暖暖视频高清图片免费完整版 | 成人免费淫片95视频观看网站 | 91一区二区在线观看精品 | 69热精品视频在线看影院 | 女bbbbxxx孕妇| 日韩成人在线视频 | 成人国产午夜在线视频 |