DateUtil.java
5.4 KB
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
package com.myproject.util;
import com.csvreader.CsvWriter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.myproject.Constants;
import com.myproject.bean.update.qisda.OutItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Date Utility Class used to convert Strings to Dates and Timestamps
*
* @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
* Modified by <a href="mailto:dan@getrolling.com">Dan Kibler </a>
* to correct time pattern. Minutes should be mm not MM (MM is month).
*/
public final class DateUtil {
private static Log log = LogFactory.getLog(DateUtil.class);
private static final String TIME_PATTERN = "HH:mm";
/**
* Checkstyle rule: utility classes should not have public constructor
*/
private DateUtil() {
}
/**
* Return default datePattern (MM/dd/yyyy)
*
* @return a string representing the date pattern on the UI
*/
public static String getDatePattern() {
Locale locale = LocaleContextHolder.getLocale();
String defaultDatePattern;
try {
defaultDatePattern = ResourceBundle.getBundle(Constants.BUNDLE_KEY, locale)
.getString("date.format");
} catch (MissingResourceException mse) {
defaultDatePattern = "MM/dd/yyyy";
}
return defaultDatePattern;
}
public static String getDateTimePattern() {
return DateUtil.getDatePattern() + " HH:mm:ss";
}
public static String toDateTimeString(Date aDate) {
return toDateString(aDate, getDateTimePattern());
}
public static String toDateString(Date aDate) {
return toDateString(aDate,getDatePattern());
}
public static String toDateString(long time, String aMask){
return toDateString(new Date(time),aMask);
}
public static String toDateString(Date aDate, String aMask) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate == null) {
log.warn("aDate is null!");
} else {
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
public static Date toDate(final String strDate) {
return toDate(strDate, getDatePattern());
}
public static Date toDate(String strDate,String aMask){
SimpleDateFormat df;
Date date = null;
df = new SimpleDateFormat(aMask);
if (log.isDebugEnabled()) {
log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
}
try {
date = df.parse(strDate);
} catch (ParseException pe) {
log.error("ParseException: " + pe);
//throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return (date);
}
/**
* 获取距离今天dayFromToday 天的不带时间的日期(今天是0,昨天是-1,明天是1)
*/
public static Date getNoTimeFromToday(int dayFromToday){
Calendar c = noTimeCalendar();
c.add(Calendar.DAY_OF_YEAR, dayFromToday);
return c.getTime();
}
private static Calendar noTimeCalendar(){
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND, 0);
return c;
}
/**
* 将日期添加一天,时间设置为00:00:00
*/
public static Date addOneDayNoTime(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND, 0);
c.add(Calendar.DAY_OF_YEAR, 1);
return c.getTime();
}
/**
* 日期+天数
* @param date
* @param days
* @return
*/
public static Date addDays(Date date, int days){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DAY_OF_YEAR, days);
return c.getTime();
}
public static Date getMinDate(Date date0, Date date1){
return date0.before(date1)? date0 : date1;
}
public static Date getMaxDate(Date date0, Date date1){
return date0.after(date1)? date0 : date1;
}
public static class Req{
private String seq;
private int op;
private int step;
private Map<String,String> data;
public String getSeq() {
return seq;
}
public void setSeq(String seq) {
this.seq = seq;
}
public int getOp() {
return op;
}
public void setOp(int op) {
this.op = op;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
public Map<String, String> getData() {
return data;
}
public void setData(Map<String, String> data) {
this.data = data;
}
}
public static void main(String args[]) throws Exception {
}
}