服务器之家

服务器之家 > 正文

Java编程时间日期API实例解析

时间:2021-03-17 13:56     来源/作者:yanweiqi

本文实例主要是关于Java8中的新特性,时间日期api的相关实例,具体如下:

?
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
275
276
277
278
package com.effective.common.base.date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
/**
 * 日期工具类
 * @author yanweiqi
 * @since 2016-5-6
 *
 */
public class LocalDateUtils {
    private static ZoneId zone = ZoneId.systemDefault();
    /**
   * 字符串转Date
   * @param date
   * @return
   * @throws Exception
   */
    public static Date convertToDate(String date) throws Exception{
        LocalDate localDate = null;
        if(null == date){
            throw new NullPointerException("date isn't null");
        } else {
            localDate = LocalDate.parse(date);
            return convertToDate(localDate);
        }
    }
    /**
   * 字符串转LocalDateTime
   * @param date
   * @return localDateTime
   */
    public static LocalDateTime convertToLocalDateTime(String date){
        LocalDateTime localDateTime = null;
        if(null == date){
            throw new NullPointerException("date isn't null");
        } else {
            localDateTime = LocalDateTime.parse(date);
            return localDateTime;
        }
    }
    /**
   * LocalDate转Date
   * @param localDate
   * @return Date
   */
    public static Date convertToDate(LocalDate localDate){
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        return Date.from(instant);
    }
    /**
   * LocalDate转Date
   * @param localDateTime
   * @return Date
   */
    public static Date convertToDate(LocalDateTime localDateTime){
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }
    /**
   * Date转LocalDate
   * @param date
   * @return localDate
   */
    public static LocalDate convertToLocalDate(Date date){
        Instant instant = date.toInstant();
        return convertToLocalDateTime(instant).toLocalDate();
    }
    /**
   * Date转LocalTime
   * @param date
   * @return localDate
   */
    public static LocalTime convertToLocalTime(Date date){
        Instant instant = date.toInstant();
        return convertToLocalDateTime(instant).toLocalTime();
    }
    /**
   * Date转LocalDatetime
   * @param date
   * @return localDate
   */
    public static LocalDateTime convertToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        return convertToLocalDateTime(instant);
    }
    /**
   * Instant转LocalDateTime
   * @param instant
   * @return
   */
    public static LocalDateTime convertToLocalDateTime(Instant instant){
        return LocalDateTime.ofInstant(instant, zone);
    }
    /**
   * LocalDateTime转Instant
   * @param localDateTime
   * @return
   */
    public static Instant convertToInstant(LocalDateTime localDateTime){
        return localDateTime.atZone(zone).toInstant();
    }
    /**
   * LocalDate转Instant
   * @param localDate
   * @return
   */
    public static Instant convertToInstant(LocalDate localDate){
        return localDate.atStartOfDay(zone).toInstant();
    }
    /**
   * LocalDate转LocalDateTime
   * @param localDate
   * @return LocalDateTime
   */
    public static LocalDateTime convertToLocalDateTime(LocalDate localDate){
        return localDate.atStartOfDay();
    }
    /**
   * 日周期格式化
   * @param localDateTime
   * @param formatStyle
   * @return
   */
    public static String formatter(LocalDateTime localDateTime, String formatStyle){
        return DateTimeFormatter.ofPattern(formatStyle).format(localDateTime);
    }
    /**
   * 设置年
   * @param sourceDate
   * @param year
   * @return LocalDateTime
   */
    public static LocalDateTime setYear(LocalDateTime sourceDate, Integer year){
        return sourceDate.withYear(year);
    }
    /**
   * 设置月
   * @param sourceDate
   * @param month
   * @return LocalDateTime
   */
    public static LocalDateTime setMonth(LocalDateTime sourceDate, Integer month){
        return sourceDate.withMonth(month);
    }
    /**
   * 设置天
   * @param sourceDate
   * @param month
   * @return LocalDateTime
   */
    public static LocalDateTime setDayOfMonth(LocalDateTime sourceDate, Integer dayOfMonth){
        return sourceDate.withDayOfMonth(dayOfMonth);
    }
    /**
   * 设置小时
   * @param sourceDate
   * @param hour
   * @return
   */
    public static LocalDateTime setHour(LocalDateTime sourceDate,Integer hour){
        return sourceDate.withHour(hour);
    }
    /**
   * 设置分钟
   * @param sourceDate
   * @param minute
   * @return
   */
    public static LocalDateTime setMinute(LocalDateTime sourceDate,Integer minute){
        return sourceDate.withMinute(minute);
    }
    /**
   * 设置秒
   * @param sourceDate
   * @param second
   * @return
   */
    public static LocalDateTime setSecond(LocalDateTime sourceDate,Integer second){
        return sourceDate.withSecond(second);
    }
    /**
   * 修改年月日
   * @param sourceDate
   * @param year
   * @param month
   * @param dayOfMonth
   * @return
   */
    public static LocalDateTime setYMD(LocalDateTime sourceDate, Integer year, Integer month, Integer dayOfMonth) {
        return sourceDate.withYear(year).withMonth(month).withDayOfMonth(dayOfMonth);
    }
    /**
   * 修改时分秒
   * @param sourceDate
   * @param hour
   * @param minute
   * @param second
   * @return
   */
    public static LocalDateTime setHMS(LocalDateTime sourceDate,Integer hour, Integer minute, Integer second) {
        return sourceDate.withHour(hour).withMinute(minute).withSecond(second);
    }
    /**
   * 计算相差的天数
   * @param beginDate
   * @param endDate
   * @return
   */
    public static int getInteverDays(LocalDate beginDate,LocalDate endDate){
        Period period = Period.between(beginDate, endDate);
        return period.getDays();
    }
    /**
   * 日期加减
   * @param num 数量
   * @param unit 单位
   * @param LocalDate 原日期
   * @return LocalDate 增加后的日期
   */
    @SuppressWarnings("static-access")
      public static LocalDate addLocalDate(long num,ChronoUnit unit,final LocalDate localDate){
        LocalDate resultDate;
        if(num > 0){
            resultDate = localDate.now().plus(num, unit);
        } else {
            resultDate = localDate.now().minus(Math.abs(num), unit);
        }
        return resultDate;
    }
    /**
   * 日期时分秒加
   * @param num 数量
   * @param unit 单位
   * @param localDateTime 原日期
   * @return LocalDateTime 增加后的日期
   */
    @SuppressWarnings("static-access")
      public static LocalDateTime addLocalDateTime(long num,ChronoUnit unit,LocalDateTime localDateTime){
        LocalDateTime resultDateTime;
        if(num > 0){
            resultDateTime = localDateTime.now().plus(num, unit);
        } else {
            resultDateTime = localDateTime.now().minus(Math.abs(num),unit);
        }
        return resultDateTime;
    }
    /**
   * 时分秒加减
   * @param num 数量
   * @param unit 单位
   * @param localTime 原日期
   * @return LocalDateTime 增加后的日期
   */
    @SuppressWarnings("static-access")
      public static LocalTime addLocalTime(long num,ChronoUnit unit,LocalTime localTime){
        LocalTime resultTime;
        if(num > 0){
            resultTime = localTime.now().plus(num, unit);
        } else {
            resultTime = localTime.now().minus(Math.abs(num), unit);
        }
        return resultTime;
    }
    public static void main(String[] args){
        LocalDateTime time = LocalDateTime.now();
        String rr = formatter(time, "yyyy-MM-dd HH:mm:ss");
        System.out.println(rr);
        LocalDateTime time2 = addLocalDateTime(-2, ChronoUnit.HOURS, time);
        String yy = formatter(time2, "yyyy-MM-dd HH:mm:ss");
        System.out.println(yy);
    }

代码涉及时间日期类的使用等内容,具有简单注释,大家可自行参考。

总结

以上就是本文关于Java编程时间日期API实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://www.cnblogs.com/ywqbj/p/5645910.html

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部