KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > conversion > DateConversionSupport


1 /*
2  * Copyright (C) 2005 db4objects Inc. http://www.db4o.com
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * db4objects - Initial API and implementation
11  */

12 package org.eclipse.core.internal.databinding.conversion;
13
14 import java.text.ParsePosition JavaDoc;
15 import java.util.Date JavaDoc;
16
17 import org.eclipse.core.internal.databinding.BindingMessages;
18
19 import com.ibm.icu.text.DateFormat;
20 import com.ibm.icu.text.SimpleDateFormat;
21
22 /**
23  * Base support for date/string conversion handling according to the default
24  * locale or in plain long milliseconds.
25  * <p>
26  * NOTE: parse(format(date)) will generally *not* be equal to date, since the
27  * string representation may not cover the sub-second range, time-only string
28  * representations will be counted from the beginning of the era, etc.
29  * </p>
30  */

31 public abstract class DateConversionSupport {
32     private final static int DATE_FORMAT=DateFormat.SHORT;
33     private final static int DEFAULT_FORMATTER_INDEX=0;
34
35     private final static int NUM_VIRTUAL_FORMATTERS=1;
36     
37     /**
38      * Alternative formatters for date, time and date/time.
39      * Raw milliseconds are covered as a special case.
40      */

41     // TODO: These could be shared, but would have to be synchronized.
42
private DateFormat[] formatters = {
43             new SimpleDateFormat(BindingMessages.getString("DateFormat_DateTime")), //$NON-NLS-1$
44
new SimpleDateFormat(BindingMessages.getString("DateFormat_Time")), //$NON-NLS-1$
45
DateFormat.getDateTimeInstance(DATE_FORMAT, DateFormat.SHORT),
46             DateFormat.getDateInstance(DATE_FORMAT),
47             DateFormat.getTimeInstance(DateFormat.SHORT),
48             DateFormat.getDateTimeInstance(DATE_FORMAT,DateFormat.MEDIUM),
49             DateFormat.getTimeInstance(DateFormat.MEDIUM)
50     };
51     
52     /**
53      * Tries all available formatters to parse the given string according to the
54      * default locale or as a raw millisecond value and returns the result of the
55      * first successful run.
56      *
57      * @param str A string specifying a date according to the default locale or in raw milliseconds
58      * @return The parsed date, or null, if no available formatter could interpret the input string
59      */

60     protected Date JavaDoc parse(String JavaDoc str) {
61         for (int formatterIdx = 0; formatterIdx < formatters.length; formatterIdx++) {
62             Date JavaDoc parsed=parse(str,formatterIdx);
63             if(parsed!=null) {
64                 return parsed;
65             }
66         }
67         return null;
68     }
69
70     protected Date JavaDoc parse(String JavaDoc str,int formatterIdx) {
71         if(formatterIdx>=0) {
72                 ParsePosition JavaDoc pos=new ParsePosition JavaDoc(0);
73                 if (str == null) {
74                     return null;
75                 }
76                 Date JavaDoc date=formatters[formatterIdx].parse(str,pos);
77                 if(pos.getErrorIndex()!=-1||pos.getIndex()!=str.length()) {
78                     return null;
79                 }
80                 return date;
81         }
82         try {
83             long millisecs=Long.parseLong(str);
84             return new Date JavaDoc(millisecs);
85         }
86         catch(NumberFormatException JavaDoc exc) {
87         }
88         return null;
89     }
90     
91     /**
92      * Formats the given date with the default formatter according to the default locale.
93      * @param date a date
94      * @return a string representation of the given date according to the default locale
95      */

96     protected String JavaDoc format(Date JavaDoc date) {
97         return format(date,DEFAULT_FORMATTER_INDEX);
98     }
99
100     protected String JavaDoc format(Date JavaDoc date,int formatterIdx) {
101         if(formatterIdx>=0) {
102             return formatters[formatterIdx].format(date);
103         }
104         return String.valueOf(date.getTime());
105     }
106
107     protected int numFormatters() {
108         return formatters.length+NUM_VIRTUAL_FORMATTERS;
109     }
110     
111     /**
112      * Returns the date format for the provided <code>index</code>.
113      * <p>
114      * This is for testing purposes only and should not be a part of the API if
115      * this class was to be exposed.
116      * </p>
117      *
118      * @param index
119      * @return date format
120      */

121     protected DateFormat getDateFormat(int index) {
122         if (index < 0 || index >= formatters.length) {
123             throw new IllegalArgumentException JavaDoc("'index' [" + index + "] is out of bounds."); //$NON-NLS-1$//$NON-NLS-2$
124
}
125         
126         return formatters[index];
127     }
128 }
Popular Tags