DateOldUtils.java
3.72 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
package com.glsx.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateOldUtils {
// public static String Sdf1ToSdf2() {
//
// }
public final static SimpleDateFormat SDF1 = new SimpleDateFormat("yyyyMMddHHmmss");
public final static SimpleDateFormat SDF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// yyyyMMddHHmmss 日期增加year年
public static String convertTime1(String time, int year) {
try {
SimpleDateFormat SDF1 = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(SDF1.parse(time));
calendar.add(Calendar.YEAR, year);
return SDF1.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
// yyyyMMddHHmmss 日期增加year年
public static String convertTime1(String time, int year, int mon) {
try {
SimpleDateFormat SDF1 = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(SDF1.parse(time));
calendar.add(Calendar.YEAR, year);
calendar.add(Calendar.MONTH, mon);
return SDF1.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
// yyyy-MM-dd HH:mm:ss
public static String convertTime2(String time, int year) {
try {
SimpleDateFormat SDF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(SDF2.parse(time));
calendar.add(Calendar.YEAR, year);
return SDF2.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
// yyyy-MM-dd HH:mm:ss
public static String convertTime2(String time, int year, int mon) {
try {
SimpleDateFormat SDF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(SDF2.parse(time));
calendar.add(Calendar.YEAR, year);
calendar.add(Calendar.MONTH, mon);
return SDF2.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
// yyyyMMddHHmmss --> yyyy-MM-dd HH:mm:ss
public static String date1ToDate2(String time) {
try {
SimpleDateFormat SDF1 = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat SDF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return SDF2.format(SDF1.parse(time));
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws ParseException {
SimpleDateFormat SDF1 = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat SDF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time1 = "20201026113900";
String time2 = "2018-10-26 11:39:00";
// System.out.println(SDF1.format(SDF1.parse(time1)));
// System.out.println(SDF2.format(SDF1.parse(time1)));
//
//
// Calendar calendar = Calendar.getInstance();
// calendar.setTime(new Date());
// calendar.add(Calendar.YEAR, -1);
//
// Date time = calendar.getTime();
// System.out.println(SDF2.format(time));
System.out.println(convertTime1(time1,-1, 2));
System.out.println(convertTime2(time2,-2));
// System.out.println(date1ToDate2("20201026113900"));
}
}