1 package com.foxinmy.weixin4j.util;
2
3 import java.math.BigDecimal;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7 import java.util.TimeZone;
8
9
10
11
12
13
14
15
16
17
18 public final class DateUtil {
19 private static final String yyyyMMdd = "yyyyMMdd";
20 private static final String yyyy_MM_dd = "yyyy-MM-dd";
21 private static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
22
23
24
25
26
27
28
29
30 public static String fortmat2yyyyMMdd(Date date) {
31 return formatDate(date, yyyyMMdd);
32 }
33
34
35
36
37
38
39
40
41 public static String fortmat2yyyy_MM_dd(Date date) {
42 return formatDate(date, yyyy_MM_dd);
43 }
44
45
46
47
48
49
50
51
52 public static String fortmat2yyyyMMddHHmmss(Date date) {
53 return formatDate(date, yyyyMMddHHmmss);
54 }
55
56
57
58
59
60
61
62
63
64
65 public static String formatDate(Date date, String pattern) {
66 SimpleDateFormat df = new SimpleDateFormat(pattern);
67 df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
68 return df.format(date);
69 }
70
71
72
73
74
75
76
77
78 public static Date parse2yyyyMMddHHmmss(String date) {
79 return parseDate(date, yyyyMMddHHmmss);
80 }
81
82
83
84
85
86
87
88
89
90
91 public static Date parseDate(String date, String pattern) {
92 SimpleDateFormat df = new SimpleDateFormat(pattern);
93 df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
94 try {
95 return df.parse(date);
96 } catch (ParseException e) {
97 throw new IllegalArgumentException(e);
98 }
99 }
100
101
102
103
104
105
106 public static String timestamp2string() {
107 return String.valueOf(System.currentTimeMillis() / 1000);
108 }
109
110
111
112
113
114
115
116
117 public static int formatYuan2Fen(double fee) {
118 BigDecimal _fee = new BigDecimal(Double.toString(fee));
119 return _fee.multiply(new BigDecimal("100"))
120 .setScale(0, BigDecimal.ROUND_HALF_EVEN).intValue();
121 }
122
123
124
125
126
127
128
129
130 public static double formatFee2Yuan(int fee) {
131 BigDecimal _fee = new BigDecimal(Integer.toString(fee));
132 return _fee.divide(new BigDecimal("100"))
133 .setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
134 }
135 }