DateFormat.java
3.3 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
package com.framework.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 时间 日期 与字符串 转换处理工具类
*
* @author s
*
*/
public class DateFormat {
/**
* 获取当前年月日字符串 20130708
*
* @return
*/
public static String getYmd() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
return "" + year + (month < 10 ? "0" + month : month) + (day < 10 ? "0" + day : day);
}
/**
* 把字符串转换成Date
*
* @param date
* @param format
* @return
* @throws ParseException
*/
public static Date formatDate(String dateSource, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
Date date = null;
try {
date = simpleDateFormat.parse(dateSource);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 把字Date换成Date
*
* @param date
* @param format
* @return
* @throws ParseException
*/
public static Date formatDate(Date dateSource, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
Date date = null;
try {
date = simpleDateFormat.parse(formatString(dateSource, format));
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 把日期转换成特定字符串
*
* @param dateSource
* @param format
* @return
* @throws ParseException
*/
public static String formatString(Date dateSource, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
String str = null;
str = simpleDateFormat.format(dateSource);
return str;
}
/**
* 把日期转换成特定字符串
*
* @param dateSource
* @param format
* @return
* @throws ParseException
*/
public static String formatString(String dateSource, String format) {
Date d = formatDate(dateSource, format);
String str = null;
str = formatString(d, format);
return str;
}
/**
* 查询上一个月时间
*
* @return
*/
public static Date getBeforeMonth() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
return formatDate(calendar.getTime(), "yyyy-MM");
}
/**
* 查询上下个月时间
*
* @return
*/
public static Date getAfterMonth() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
return formatDate(calendar.getTime(), "yyyy-MM");
}
/**
* 查询上下个月时间
*
* @return
*/
public static Date getAfterMonth(Date targetDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(targetDate);
calendar.add(Calendar.MONTH, 1);
return formatDate(calendar.getTime(), "yyyy-MM");
}
/**
* 查询月
*
* @return
*/
public static Date getMonth(Date targetDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(targetDate);
return formatDate(calendar.getTime(), "yyyy-MM");
}
public static void main(String[] args) {
System.out.println(getAfterMonth(new Date()));
}
}