Repository navigation
Expand file tree
/
Copy pathDateFormatUtil.java
More file actions
150 lines (137 loc) · 3.38 KB
/
Copy pathDateFormatUtil.java
File metadata and controls
150 lines (137 loc) · 3.38 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
package test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* 日期格式转换工具类
* String2Date或者Date2String
* 针对不同的日期格式进行相互转换
* @author ggh
*
*/
public class DateFormatUtil {
private static final String PATTERN_DATE = "yyyy-MM-dd";
private static final String PATTERN_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
public static String GMTDateToSimpleDateString(String GMTDateString){
java.text.SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.US);
Date d;
try {
d = sdf.parse(GMTDateString);
sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String format(Date date, String pattern) {
if (date == null) {
return null;
} else {
try {
return null;
//return DateFormatUtils.format(date, pattern);
} catch (Exception e) {
return null;
}
}
}
public static String formatDate(Date date) {
return format(date, PATTERN_DATE);
}
public static String formatDateTime(Date date) {
return format(date, PATTERN_DATE_TIME);
}
public static Date stringToDate(String date) {
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date time=null;
try {
if(date!=null&&!"".equals(date)){
time = format.parse(date);
}else{
time = null;
}
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
public static Date stringToDatetime(String date) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date time = null;
try {
if(date!=null&&!"".equals(date)){
time = format.parse(date);
}else{
time = null;
}
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
public static Date stringToDateFormat(String date) {
DateFormat format=new SimpleDateFormat("yyyy/MM/dd");
Date time=null;
try {
if(date!=null&&!"".equals(date)){
time = format.parse(date);
}else{
time = null;
}
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
public static Date stringToDatetime(String date, String pattern) {
DateFormat format = new SimpleDateFormat(pattern);
Date time = null;
try {
if(date!=null&&!"".equals(date)){
time = format.parse(date);
}else{
time = null;
}
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
/**
* 获取系统当前时间
* @return 返回规定格式的数据 例如:20140710170735665
*/
public static String formatString(){
return String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS%1$tL", System.currentTimeMillis());
}
/**
* 获取当前年
* @return yyyy
*/
public static String getYear() {
SimpleDateFormat format = new SimpleDateFormat("yyyy");
Date date = new Date();
return format.format(date).toString();
}
/**
* 获取当前月
* @return MM
*/
public static String getMonth() {
SimpleDateFormat format = new SimpleDateFormat("MM");
Date date = new Date();
return format.format(date).toString();
}
/**
* 获取当前日
* @return dd
*/
public static String getDay(){
SimpleDateFormat format = new SimpleDateFormat("dd");
Date date = new Date();
return format.format(date).toString();
}
}