13 Kasım 2014 Perşembe

JAVA DATE FORMAT VE XMLGregorianCalendar Dönüşümü

GregorianCalendar
 
 


DateAdapter.java



DateAdapter.java
 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 BeytullahC
12  */
13 public class DateAdapter {
14 
15     /**
16      * DateFormatter is used to convert string to date. String pattern is
17      * 'yyyy-MM-dd hh:mm:ss'
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 SimpleDateFormat yyyy-MM-dd hh:mm:ss
26      */
27     public static SimpleDateFormat getDateFormatter() {
28         return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
29     }
30 
31     /**
32      *
33      * @return SimpleDateFormat yyyyMMdd
34      */
35     public static SimpleDateFormat getProjectDateFormatter() {
36         return new SimpleDateFormat("yyyyMMdd");
37     }
38 
39     /**
40      *
41      * @return format SimpleDateFormat yyyyMMddHHmm
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