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

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 學習Java的Date、Calendar日期操作

學習Java的Date、Calendar日期操作

2019-12-31 14:25白小衣 JAVA教程

Java開發過程中避免不了日期相關操作,這篇文章總結了一些Date、Calendar的常用方法,需要的朋友可以參考下

本文介紹了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
package jse;
 
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
 
/**
 * 常用日期操作
 *
 * @author puck
 * createDate 2015-07-30 22:54:38
 */
public class TestDate
{
  public static void main(String[] args) throws ParseException, UnsupportedEncodingException
  {
    Calendar cal = Calendar.getInstance();
//   cal.add(Calendar.DAY_OF_MONTH, -48);
    System.out.println(dateToString(cal));
  }
   
  /**
   * 日期格式化
   * @param date
   * @return
   */
  public static String dateToString(Date date)
  {
//   SimpleDateFormat format = new SimpleDateFormat("y年MM月dd日 E HH時mm分ss秒", Locale.CHINA);
//   SimpleDateFormat format = new SimpleDateFormat("y年M月d日 E H時m分s秒", Locale.CHINA);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); // example
    return sdf.format(date);
  }
   
  /**
   * 日期格式化
   * @param date
   * @return
   */
  public static String dateToString(Calendar cal)
  {
    return dateToString(cal.getTime());
  }
 
  /**
   * dateString 轉 Calendar
   *
   * @param Date
   *      format:2015-06-16 date
   * @return Calendar
   * @throws ParseException
   */
  public static Calendar dateStringToCalendar(String dateStr) throws ParseException
  {
//   Calendar cal = Calendar.getInstance();
//   cal.clear();
//   cal.set(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(5, 7)) - 1,
//       Integer.parseInt(date.substring(8, 10)));
//   return cal;
     
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = sdf.parse(dateStr);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal;
  }
 
  /**
   * dateString 轉 Date
   *
   * @param Date
   *      format:yyyy-MM-dd HH:mm:ss date
   * @return Calendar
   * @throws ParseException
   */
  public static Date dateStringToDate(String date) throws ParseException
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.parse(date);
  }
 
  /**
   * Date Convert to Calendar
   *
   * @param date
   * @return
   */
  public static Calendar dateToCalendar(Date date)
  {
    Calendar c1 = Calendar.getInstance();
    c1.setTime(date);
    return c1;
  }
 
  /**
   * Calendar Convert To Date
   * @param cal
   * @return
   */
  public static Date CalendarToDate(Calendar cal)
  {
    return cal.getTime();
  }
 
  /**
   * 計算兩個日期相差年月日
   *
   * @param Date
   *      c1
   * @param Date
   *      c2
   * @return int[]{year, month, day}
   */
  public int[] calculateDifferDay(Date d1, Date d2)
  {
    Calendar c1 = Calendar.getInstance();
    c1.setTime(d1);
    Calendar c2 = Calendar.getInstance();
    c1.setTime(d2);
    return calculateDifferDay(c1, c2);
  }
 
  /**
   * 計算兩個日期相差年月日
   *
   * @param Calendar
   *      c1
   * @param Calendar
   *      c2
   * @return int[]{year, month, day}
   */
  public int[] calculateDifferDay(Calendar c1, Calendar c2)
  {
    int[] p1 = { c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH) };
    int[] p2 = { c2.get(Calendar.YEAR), c2.get(Calendar.MONTH), c2.get(Calendar.DAY_OF_MONTH) };
    System.out.println("p1[0]=" + p1[0] + " p1[1]=" + p1[1] + " p1[2]=" + p1[2]);
    System.out.println("p2[0]=" + p2[0] + " p2[1]=" + p2[1] + " p2[2]=" + p2[2]);
    int year = p2[0] - p1[0];
    int month = (p2[0] * 12) + p2[1] - ((p1[0] * 12) + p1[1]);
    int day = (int) ((c2.getTimeInMillis() - c1.getTimeInMillis()) / (24 * 60 * 60 * 1000));
    return new int[] { year, month, day };
  }
 
  /**
   * 獲取日期所在周的第一天
   *
   * @param c
   * @return
   */
  public static Calendar getLastDayOfWeek(Calendar c)
  {
//   SimpleDateFormat format2 = new SimpleDateFormat("y年M月d日 E H時m分s秒", Locale.CHINA);
//   System.out.println("當前時間:" + format2.format(c.getTime()));
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
//   System.out.println("周一時間:" + format2.format(c.getTime()));
    return c;
  }
 
  /**
   * 日期加減
   * @param c
   * @param day
   * @return
   */
  public static Calendar addOrDecreaseDay(Calendar c, int day)
  {
    c.add(Calendar.DAY_OF_MONTH, day);
    return c;
  }
 
  /**
   * 獲取月最后一天
   * @param year
   * @param month
   * @return
   */
  public static int getLastDayOfMonth(int year, int month)
  {
    Calendar c = Calendar.getInstance();
    c.set(year, month - 1, 1);
    return c.getActualMaximum(Calendar.DAY_OF_MONTH);
  }
   
  /**
   * 獲取月最后一天
   * @param cal
   * @return
   */
  public static int getLastDayOfMonth(Calendar cal)
  {
    return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  }
   
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 视频大全在线观看网址 | 亚州成人| 欧美在线视频一区在线观看 | 99久久er这里只有精品17 | 国产精品51麻豆cm传媒 | 成人啪啪漫画全文阅读 | 手机看片福利盒子久久 | 亚洲精品久久久成人 | blacked黑人| 成人啪啪漫画羞羞漫画www网站 | 天堂中文在线免费观看 | ai换脸杨颖啪啪免费网站 | 国产馆在线观看免费的 | 金发美女与黑人做爰 | 亚洲人成网站在线观看播放青青 | 国内精品91最新在线观看 | 性插图动态图无遮挡 | 青青色综合 | 国产极品久久 | 亚洲精品久久久992KVTV | www在线观看视频免费 | 免费日批视频 | 男人女人性生活视频 | 亚洲 欧美 日韩 综合 | 精品欧美 | 成人免费观看一区二区 | 国产精品va在线观看无 | 亚洲丰满模特裸做爰 | haodiaose在线精品免费视频 | 无码国产成人午夜在线观看不卡 | av魔镜收集号 | 国语刺激对白勾搭视频在线观看 | 高中生喷水喷浆 | 亚裔maricahase和黑人 | 久久九九有精品国产23百花影院 | 免费成人在线观看视频 | 校花被强迫np肉高h 校服下的白嫩小乳尖h1v1 | 男生同性视频twink在线 | 国产成人精品在线观看 | 99在线视频免费 | 国产一区二区不卡视频 |