GregorianCalendar
DateAdapter.java
body {color: #000000; background-color: #ffffff; font-family: Monospaced}
pre {color: #000000; background-color: #ffffff; font-family: Monospaced}
table {color: #000000; background-color: #e9e8e2; font-family: Monospaced}
.ST4 {font-family: Monospaced; font-weight: bold; font-style: italic}
.ST3 {font-family: Monospaced; font-style: italic}
.ST1 {font-family: Monospaced; font-weight: bold}
.ST0 {color: #969696; font-family: Monospaced; font-weight: bold}
.comment {color: #969696}
.character {color: #ce7b00}
.ST2 {color: #009900; font-family: Monospaced; font-style: italic}
.keyword-directive {color: #0000e6}
1 import java.text.ParseException;
2 import java.text.SimpleDateFormat;
3 import java.util.Calendar;
4 import java.util.Date;
5 import java.util.GregorianCalendar;
6 import java.util.TimeZone;
7 import javax.xml.bind.DatatypeConverter;
9
10
11 @author
12
13 public class DateAdapter {
14
15
16 DateFormatter is used to convert string to date.
17
18
19 @return
20
21 private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DateAdapter.class);
22
23
24
25 @return
26
27 public static SimpleDateFormat getDateFormatter() {
28 return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
29 }
30
31
32
33 @return
34
35 public static SimpleDateFormat getProjectDateFormatter() {
36 return new SimpleDateFormat("yyyyMMdd");
37 }
38
39
40
41 @return
42
43 public static SimpleDateFormat getDateLongFormatter() {
44 return new SimpleDateFormat("yyyyMMddHHmm");
45 }
46
47 public static Date convertStringToDate(String dateStr, SimpleDateFormat formatter) throws ParseException {
48 try {
49 if (dateStr != null && !"".equals(dateStr)) {
50 formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
51 return formatter.parse(dateStr);
52
53 }
54 } catch (ParseException e) {
55 logger.error("ParseException", e);
56 }
57 return null;
58 }
59
60 public static String convertDateToString(Date date, SimpleDateFormat formatter) {
61 try {
62 if (date != null) {
63 String YYYYMMDD = formatter.format(date);
64 return YYYYMMDD;
65 }
66 } catch (Exception e) {
67 logger.error(e.getMessage(), e);
68 }
69 return null;
70 }
71
72 public static Date parseDate(String s) {
73 return DatatypeConverter.parseDate(s).getTime();
74 }
75
76 public static String printDate(Date dt) {
77 Calendar cal = new GregorianCalendar();
78 cal.setTime(dt);
79 return DatatypeConverter.printDate(cal);
80 }
81
82 }
83
Hiç yorum yok:
Yorum Gönder