KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

87     protected String JavaDoc format(Date JavaDoc date) {
88         return format(date,DEFAULT_FORMATTER_INDEX);
89     }
90
91     protected String JavaDoc format(Date JavaDoc date,int formatterIdx) {
92         if(formatterIdx>=0) {
93             return formatters[formatterIdx].format(date);
94         }
95         return String.valueOf(date.getTime());
96     }
97
98     protected int numFormatters() {
99         return formatters.length+NUM_VIRTUAL_FORMATTERS;
100     }
101 }
Popular Tags