78
|
1 /*
|
|
2 * Copyright cast(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 * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940
|
|
12 */
|
|
13 module org.eclipse.core.internal.databinding.conversion.DateConversionSupport;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.text.ParsePosition;
|
|
18 import java.util.Date;
|
|
19
|
|
20 import org.eclipse.core.internal.databinding.BindingMessages;
|
|
21
|
|
22 import com.ibm.icu.text.DateFormat;
|
|
23 import com.ibm.icu.text.SimpleDateFormat;
|
|
24
|
|
25 /**
|
|
26 * Base support for date/string conversion handling according to the default
|
|
27 * locale or in plain long milliseconds.
|
|
28 * <p>
|
|
29 * NOTE: parse(format(date)) will generally *not* be equal to date, since the
|
|
30 * string representation may not cover the sub-second range, time-only string
|
|
31 * representations will be counted from the beginning of the era, etc.
|
|
32 * </p>
|
|
33 */
|
|
34 public abstract class DateConversionSupport {
|
|
35 private final static int DATE_FORMAT=DateFormat.SHORT;
|
|
36 private final static int DEFAULT_FORMATTER_INDEX=0;
|
|
37
|
|
38 private final static int NUM_VIRTUAL_FORMATTERS=1;
|
|
39
|
|
40 /**
|
|
41 * Alternative formatters for date, time and date/time.
|
|
42 * Raw milliseconds are covered as a special case.
|
|
43 */
|
|
44 // TODO: These could be shared, but would have to be synchronized.
|
|
45 private DateFormat[] formatters = {
|
|
46 new SimpleDateFormat(BindingMessages.getStringcast(BindingMessages.DATE_FORMAT_DATE_TIME)),
|
|
47 new SimpleDateFormat(BindingMessages.getStringcast(BindingMessages.DATEFORMAT_TIME)),
|
|
48 DateFormat.getDateTimeInstance(DATE_FORMAT, DateFormat.SHORT),
|
|
49 DateFormat.getDateInstancecast(DATE_FORMAT),
|
|
50 DateFormat.getTimeInstancecast(DateFormat.SHORT),
|
|
51 DateFormat.getDateTimeInstance(DATE_FORMAT,DateFormat.MEDIUM),
|
|
52 DateFormat.getTimeInstancecast(DateFormat.MEDIUM)
|
|
53 };
|
|
54
|
|
55 /**
|
|
56 * Tries all available formatters to parse the given string according to the
|
|
57 * default locale or as a raw millisecond value and returns the result of the
|
|
58 * first successful run.
|
|
59 *
|
|
60 * @param str A string specifying a date according to the default locale or in raw milliseconds
|
|
61 * @return The parsed date, or null, if no available formatter could interpret the input string
|
|
62 */
|
|
63 protected Date parse(String str) {
|
|
64 for (int formatterIdx = 0; formatterIdx < formatters.length; formatterIdx++) {
|
|
65 Date parsed=parse(str,formatterIdx);
|
|
66 if(parsed !is null) {
|
|
67 return parsed;
|
|
68 }
|
|
69 }
|
|
70 return null;
|
|
71 }
|
|
72
|
|
73 protected Date parse(String str,int formatterIdx) {
|
|
74 if(formatterIdx>=0) {
|
|
75 ParsePosition pos=new ParsePosition(0);
|
|
76 if (str is null) {
|
|
77 return null;
|
|
78 }
|
|
79 Date date=formatters[formatterIdx].parse(str,pos);
|
|
80 if(pos.getErrorIndex() !is -1||pos.getIndex() !is str.length()) {
|
|
81 return null;
|
|
82 }
|
|
83 return date;
|
|
84 }
|
|
85 try {
|
|
86 long millisecs=Long.parseLong(str);
|
|
87 return new Date(millisecs);
|
|
88 }
|
|
89 catch(NumberFormatException exc) {
|
|
90 }
|
|
91 return null;
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * Formats the given date with the default formatter according to the default locale.
|
|
96 * @param date a date
|
|
97 * @return a string representation of the given date according to the default locale
|
|
98 */
|
|
99 protected String format(Date date) {
|
|
100 return format(date,DEFAULT_FORMATTER_INDEX);
|
|
101 }
|
|
102
|
|
103 protected String format(Date date,int formatterIdx) {
|
|
104 if(formatterIdx>=0) {
|
|
105 return formatters[formatterIdx].format(date);
|
|
106 }
|
|
107 return String.valueOf(date.getTime());
|
|
108 }
|
|
109
|
|
110 protected int numFormatters() {
|
|
111 return formatters.length+NUM_VIRTUAL_FORMATTERS;
|
|
112 }
|
|
113
|
|
114 /**
|
|
115 * Returns the date format for the provided <code>index</code>.
|
|
116 * <p>
|
|
117 * This is for testing purposes only and should not be a part of the API if
|
|
118 * this class was to be exposed.
|
|
119 * </p>
|
|
120 *
|
|
121 * @param index
|
|
122 * @return date format
|
|
123 */
|
|
124 protected DateFormat getDateFormat(int index) {
|
|
125 if (index < 0 || index >= formatters.length) {
|
|
126 throw new IllegalArgumentException("'index' [" + index + "] is out of bounds."); //$NON-NLS-1$//$NON-NLS-2$
|
|
127 }
|
|
128
|
|
129 return formatters[index];
|
|
130 }
|
|
131 }
|