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

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

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

服務器之家 - 編程語言 - Java教程 - Java實現的日期處理類完整實例

Java實現的日期處理類完整實例

2021-01-05 11:07夏日娃 Java教程

這篇文章主要介紹了Java實現的日期處理類,結合完整實例形式分析了Java針對日期的獲取、運算、轉換等相關操作技巧,需要的朋友可以參考下

本文實例講述了Java實現的日期處理類。分享給大家供大家參考,具體如下:

開發中常常要使用日期,先小結如下,以備后用。

?
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
266
267
268
269
270
271
272
273
274
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class FormatTime {
  private final static SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
  private final static SimpleDateFormat sdfymdhm = new SimpleDateFormat("yyyyMMddHHmmss");
  private final static SimpleDateFormat sdfymdhms =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  /**
   *
  * @Title: getCurrentDay
  * @Description: TODO 獲取當天時間(20161109)
  * @return
   */
  public static String getCurrentDay(){
    return sdf.format(new Date());
  }
  /**
   *
  * @Title: fTime2
  * @Description: TODO 獲取time這個日期以前dayAgo天的日期
  * @return
   */
  public static String fTime(String time,int dayAgo){
    Date date = null;
    try {
      date = sdf.parse(time);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if(dayAgo>0){
      calendar.add(Calendar.DAY_OF_MONTH, -dayAgo);//前15天數據
      date = calendar.getTime();
      calendar.setTime(date);
    }
    int year=calendar.get(Calendar.YEAR);
    int month=calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    String mon="";
    String d="";
    if(month<10){
      mon="0"+month;
    }else{
      mon=month+"";
    }
    if(day<10){
      d="0"+day;
    }else{
      d=""+day;
    }
    String ret=year+""+mon+""+d;
    return ret;
  }
  /**
   *
  * @Title: fTime2
  * @Description: TODO 獲取time這個日期以后dayAfter天的日期
  * @return
   */
  public static String fTime2(String time,int dayAfter){
    Date date = null;
    try {
      date = sdf.parse(time);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if(dayAfter>0){
      calendar.add(Calendar.DAY_OF_MONTH, +dayAfter);//后15天數據
      date = calendar.getTime();
      calendar.setTime(date);
    }
    int year=calendar.get(Calendar.YEAR);
    int month=calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    String mon="";
    String d="";
    if(month<10){
      mon="0"+month;
    }else{
      mon=month+"";
    }
    if(day<10){
      d="0"+day;
    }else{
      d=""+day;
    }
    String ret=year+""+mon+""+d;
    return ret;
  }
  /**
   *
  * @Title: getDefaultTime
  * @Description: TODO 獲取昨天的日期
  * @return
   */
  public static String getDefaultTime(){
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, -1);//前1天
    Date date = calendar.getTime();
    String time=sdf.format(date);
    return time;
  }
  /**
   *
  * @Title: getSunday
  * @Description: TODO 獲取最近一個星期天
  * @return
   */
  public static String getSunday(){
    SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
    Calendar c = Calendar.getInstance();
    c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    return f.format(c.getTime());
  }
  /**
   *
  * @Title: getMonthFirstDay
  * @Description: TODO 獲取本月第一天
  * @return
   */
  public static String getCurrentMonthFirstDay(){
    Calendar  cal_1=Calendar.getInstance();//獲取當前日期
    cal_1.add(Calendar.MONTH, 0);
    cal_1.set(Calendar.DAY_OF_MONTH,1);//設置為1號,當前日期既為本月第一天
    String firstDay = sdf.format(cal_1.getTime());
    return firstDay;
  }
  /**
   *
  * @Title: getMonthFirstDay
  * @Description: TODO 獲取上月第一天
  * @return
   */
  public static String getPreviousMonthFirstDay(){
   //獲取當前月第一天:
  Calendar c = Calendar.getInstance();
  c.add(Calendar.MONTH, -1);
  c.set(Calendar.DAY_OF_MONTH,1);//設置為1號,當前日期既為本月第一天
  String first = sdf.format(c.getTime());
  return first;
  }
  /**
   *
  * @Title: getMonthFirstDay
  * @Description: TODO 獲取上月最后一天
  * @return
   */
  public static String getPreviousMonthLastDay(){
  //獲取當前月最后一天
  Calendar ca = Calendar.getInstance();
  ca.set(Calendar.DAY_OF_MONTH,0);//
  String lastDay = sdf.format(ca.getTime());
  return lastDay;
  }
  /**
   *
  * @Title: getCurrentMonthLastDay
  * @Description: TODO 獲取指定時間最后一天
  * @return
   */
  public static String getCurrentMonthLastDay(String time){
    Date date =null;
   try {
    date= sdf.parse(time);
  } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  //獲取當前月最后一天
  Calendar ca = Calendar.getInstance();
  ca.setTime(date);
  ca.set(Calendar.DAY_OF_MONTH,
      ca.getActualMaximum(Calendar.DAY_OF_MONTH)); //
  String lastDay = sdf.format(ca.getTime());
  return lastDay;
  }
  /***
   *
  * @Title: getCurrentWeekDay
  * @Description: TODO 獲取本周周一
   */
  public static String getCurrentMonday(){
     Calendar cal = Calendar.getInstance();
     cal.setFirstDayOfWeek(Calendar.MONDAY);//將每周第一天設為星期一,默認是星期天
     cal.add(Calendar.DATE, 0);
     cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
     String monday = sdf.format(cal.getTime());
     return monday;
  }
  /***
   *
  * @Title: getPreviousSunday
  * @Description: TODO 獲取上周周日
   */
  public static String getPreviousSunday(){
     Calendar cal = Calendar.getInstance();
     cal.setFirstDayOfWeek(Calendar.MONDAY);//將每周第一天設為星期一,默認是星期天
     cal.add(Calendar.DATE, -1*7);
     cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
     String sunday =sdf.format(cal.getTime());
     return sunday;
  }
  /**
   *
  * @Title: getMiniSencond
  * @Description: TODO 將日期轉換為毫秒數
  * @param str
  * @return
   */
  public static String getMiniSencond(String str){
    long millionSeconds=0;
    try {
      millionSeconds = sdfymdhm.parse(str).getTime();//毫秒
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return millionSeconds+"";
  }
   /**
   *
  * @Title: getDateSencond
  * @Description: TODO 將日期轉換為毫秒數
  * @param str
  * @return
   */
  public static long getDateSencond(String str){
    long millionSeconds=0;
    try {
      millionSeconds = sdfymdhms.parse(str).getTime();//毫秒
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return millionSeconds;
  }
  /**
   * 計算日期相差天數
   * @param str1
   * @param str2
   * @return
   */
  public static int getDistanceOfTwoDate(String str1,String str2){
    int result=0;
    try{
      Date date1 = sdf.parse(str1);
      Date date2 =sdf.parse(str2);
      Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date1);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(date2);
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        result = day1-day2;
    }catch(Exception e){
      e.printStackTrace();
    }
    return result;
  }
  /**
   *
  * @Title: long2Date
  * @Description: TODO long 轉日期(年-月-日 時-分-秒)
  * @param timestamp
  * @return
   */
  public static String longToDate(Long msecond){
    Date date = new Date(msecond);
    return sdfymdhms.format(date);
  }
}

希望本文所述對大家java程序設計有所幫助。

原文鏈接:http://blog.csdn.net/u011625492/article/details/72843471

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 四虎麻豆| 69罗莉视频在线观看 | 欧美成人v视频免费看 | 3d动漫美女物被遭强视频 | 国产欧美一区二区三区免费 | 欧美日韩亚洲另类人人澡 | 秋霞717理论片在线观看 | 嫩草视频在线观看视频播放 | 美女视频一区二区三区在线 | 亚洲男人的天堂在线 | 国产探花在线观看 | 精品久久久久久久国产潘金莲 | 国产午夜精品福利 | 色综合中文字幕在线亚洲 | 吉川爱美与黑人解禁 | 99国产在线视频 | 香蕉精品国产高清自在自线 | 成人免费影 | 亚洲精品一线二线三线 | 国产午夜精品久久理论片小说 | sao虎影院桃红视频在线观看 | 韩国成人毛片aaa黄 含羞草国产亚洲精品岁国产精品 | 暖暖在线精品日本中文 | 免费观看视频在线播放 | 久青草国产97香蕉在线视频 | 四缺一的小说 | 五月九九 | 色噜噜亚洲男人的天堂www | 全黄h全肉细节修仙玄幻文 全彩调教侵犯h本子全彩妖气he | 午夜精品久久久久久 | 娇妻与公陈峰姚瑶最新版 | 九九精品免视看国产成人 | 精品AV无码一二三区视频 | 亚洲精品老司机福利在线播放 | 女人pp被扒开流水了 | 国产好痛疼轻点好爽的视频 | 国产一区二区三区欧美 | 91国语精品自产拍在线观看一 | 欧美亚洲天堂网 | 国内会所按摩推拿国产 | 国产99久久精品一区二区 |