1 package org.opensync.tools;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.StringTokenizer;
7
8 /***
9 * An incomplete class. The class was written in the hope that it would provide
10 * all the functionalities of a date formatter. But, for now, it just provides
11 * parse function.
12 *
13 *
14 */
15 public class APDateFormatter {
16
17 public static final int AP_PDA = 0;
18 public static final int AP_DB1 = 1;
19 public static final int AP_DB2 = 2;
20
21 private SimpleDateFormat sdf;
22 private String formattedString;
23
24
25 public APDateFormatter(int formatType)
26 {
27
28 switch(formatType)
29 {
30 case AP_PDA :
31 sdf = new SimpleDateFormat("yyyyMMdd:HHmm");
32 break;
33 case AP_DB2 :
34 sdf = new SimpleDateFormat("yyyy-MM-dd:HHmm");
35 break;
36 case AP_DB1 :
37 default :
38 sdf = new SimpleDateFormat("yyyyMMdd:HHmm");
39 }
40 }
41
42 public APDateFormatter(String formatString)
43 {
44 sdf = new SimpleDateFormat(formatString);
45 }
46
47 public APDateFormatter()
48 {
49 this(AP_DB1);
50 }
51
52 public String format(Date date)
53 {
54 formattedString = sdf.format(date);
55 return formattedString;
56 }
57
58 public String getDateString()
59 {
60 StringTokenizer st = new StringTokenizer(formattedString, ":");
61 return st.nextToken();
62 }
63
64 public String getTimeString()
65 {
66 StringTokenizer st = new StringTokenizer(formattedString, ":");
67 st.nextToken();
68 return st.nextToken();
69 }
70
71 /***
72 * Parse function accepts strings in the form YYYYDDMM and parses it to a date.
73 * Feel free to add more functionality to this method to support more formats.
74 *
75 *
76 */
77 public Date parse(String str) throws ParseException
78 {
79
80
81
82
83
84
85 System.out.println("APDate :" + str);
86 return(sdf.parse(str));
87
88 }
89
90 public Date parse(String date, String time) throws ParseException
91 {
92 if (time!=null && time.length()>3) time = time.substring(0,4);
93 else time = "0800";
94 return parse(date + ":" + time);
95 }
96
97 public static void main(String[] args)
98 {
99 System.out.println(args[0] + " " + args[1]);
100 APDateFormatter ap = new APDateFormatter();
101 try
102 {
103
104 System.out.println(ap.parse(args[0], args[1]));
105 }catch(ParseException ex)
106 {
107 ex.printStackTrace();
108 }
109 }
110
111 }
112