01: /**********************************************************************************
02: * $URL$
03: * $Id$
04: ***********************************************************************************
05: *
06: * Copyright (c) 2003, 2004, 2005, 2006, 2007 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.util;
21:
22: import java.text.DateFormat;
23: import java.text.SimpleDateFormat;
24: import java.text.ParseException;
25: import java.util.*;
26:
27: import org.sakaiproject.component.cover.ServerConfigurationService;
28: import org.sakaiproject.util.ResourceLoader;
29:
30: /**
31: * Created by IntelliJ IDEA.
32: * User: johnellis
33: * Date: Mar 6, 2007
34: * Time: 7:09:56 AM
35: * To change this template use File | Settings | File Templates.
36: */
37: public class DateWidgetFormat {
38:
39: private Map<String, DateFormat> acceptableFormats;
40: private ResourceLoader loader = new ResourceLoader();
41: private Date testDate;
42:
43: public static DateFormat MM_DD_YYYY = new SimpleDateFormat(
44: "MM/dd/yyyy");
45: public static DateFormat DD_MM_YYYY = new SimpleDateFormat(
46: "dd-MM-yyyy");
47:
48: public static DateFormat MM_DD_YYYY_short = new SimpleDateFormat(
49: "M/d/yy");
50: public static DateFormat DD_MM_YYYY_short = new SimpleDateFormat(
51: "d/M/yy");
52:
53: public DateWidgetFormat() {
54: try {
55: testDate = MM_DD_YYYY.parse("12/31/1999");
56: } catch (ParseException e) {
57:
58: }
59: acceptableFormats = new Hashtable<String, DateFormat>();
60: acceptableFormats.put(MM_DD_YYYY_short.format(testDate),
61: MM_DD_YYYY);
62: acceptableFormats.put(DD_MM_YYYY_short.format(testDate),
63: DD_MM_YYYY);
64: }
65:
66: public DateFormat getLocaleDateFormat() {
67: DateFormat returned = DateFormat.getDateInstance(
68: DateFormat.SHORT, loader.getLocale());
69: String testDateString = returned.format(testDate);
70: if (acceptableFormats.containsKey(testDateString)) {
71: return acceptableFormats.get(testDateString);
72: } else {
73: return getDefaultDateFormat();
74: }
75: }
76:
77: public DateFormat getDefaultDateFormat() {
78: String defaultFormat = ServerConfigurationService
79: .getString("dateWidget.defaultFormat");
80: if ("dd/MM/yyyy".equals(defaultFormat)) {
81: return DD_MM_YYYY;
82: }
83: return MM_DD_YYYY;
84: }
85:
86: }
|