KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > shared > util > DateHelper


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tools.testrecorder.shared.util;
19
20 import org.apache.beehive.netui.tools.testrecorder.server.state.Session;
21
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.text.ParseException JavaDoc;
24
25 import java.util.Calendar JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Date JavaDoc;
28
29 /**
30  * Utility to help handle Date/Calendar formatting in the test recorder.
31  */

32 public class DateHelper
33 {
34     /**
35      * Default {@link java.util.Locale} used to format dates that the test recorder uses.
36      * This defaults to the <code>US</code> Locale for the time being so that dates
37      * are handled in an internally consistent way.
38      */

39     private static final Locale JavaDoc DEFAULT_LOCALE = Locale.US;
40
41     /**
42      * Format for how dates are parsed and written.
43      */

44     private static final String JavaDoc DATE_FORMAT_STRING = "dd MMM yyyy, hh:mm:ss.SSS aa zzz";
45
46     /**
47      * Format for how dates are parsed and written in the JSP UI for the test recorder.
48      *
49      * These are just more readable than those that are used for the test record XML files.
50      */

51     private static final String JavaDoc READABLE_DATE_FORMAT_STRING = "dd MMM, hh:mm:ss aa";
52
53     /**
54      * {@link java.text.SimpleDateFormat} used to parse Strings to {@link java.util.Date} objects.
55      */

56     private static final SimpleDateFormat JavaDoc XML_DATE_FORMAT = new SimpleDateFormat JavaDoc(DATE_FORMAT_STRING, DEFAULT_LOCALE);
57
58     /**
59      * {@link java.text.SimpleDateFormat} used to parse Strings to {@link java.util.Date} objects.
60      */

61     private static final SimpleDateFormat JavaDoc READABLE_DATE_FORMAT = new SimpleDateFormat JavaDoc(READABLE_DATE_FORMAT_STRING, DEFAULT_LOCALE);
62
63
64     // Do not construct
65
private DateHelper() {}
66
67     /**
68      * Get a {@link java.util.Calendar} instance that represents <code>now</code>.
69      *
70      * @return said Calendar instance
71      */

72     public static final Calendar JavaDoc getCalendarInstance()
73     {
74         return Calendar.getInstance(DEFAULT_LOCALE);
75     }
76
77     /**
78      * Get a {@link java.util.Calendar} instance that represents the date parsed
79      * from the <code>dateString</code> parameter.
80      *
81      * @param dateString String date to parse
82      * @return the parsed Calendar instance
83      * @throws ParseException thrown when the dateString can't be parsed
84      */

85     public static final Calendar JavaDoc getCalendarInstance(String JavaDoc dateString)
86         throws ParseException JavaDoc
87     {
88         assert dateString != null;
89
90         Calendar JavaDoc calendar = Calendar.getInstance(DEFAULT_LOCALE);
91         calendar.setTime(XML_DATE_FORMAT.parse(dateString));
92
93         return calendar;
94     }
95
96     /**
97      * Format the given {@link java.util.Calendar} instance into a String
98      * for printing / display.
99      *
100      * @param calendar {@link java.util.Calendar} instance to format.
101      * @return the formatted String
102      */

103     public static final String JavaDoc formatToString(Calendar JavaDoc calendar)
104     {
105         assert calendar != null;
106         return XML_DATE_FORMAT.format(calendar.getTime());
107     }
108
109     /**
110      * Format the given {@link java.util.Date} instance into a String
111      * for use in a test's recorded XML file.
112      *
113      * @param date {@link java.util.Date} instance to format.
114      * @return the formatted String
115      */

116     public static final String JavaDoc formatToString(Date JavaDoc date)
117     {
118         assert date != null;
119         return XML_DATE_FORMAT.format(date);
120     }
121
122     /**
123      * Format the given {@link java.util.Date} into a String for display.
124      *
125      * @param date the {@link java.util.Date} to format
126      * @return the formatted String
127      */

128     public static final String JavaDoc formatToReadableString(Date JavaDoc date)
129     {
130         assert date != null;
131         return READABLE_DATE_FORMAT.format(date);
132     }
133 }
134
Popular Tags