1 12 package org.eclipse.core.internal.databinding.conversion; 13 14 import java.text.ParsePosition ; 15 import java.util.Date ; 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 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 41 private DateFormat[] formatters = { 43 new SimpleDateFormat(BindingMessages.getString("DateFormat_DateTime")), new SimpleDateFormat(BindingMessages.getString("DateFormat_Time")), 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 60 protected Date parse(String str) { 61 for (int formatterIdx = 0; formatterIdx < formatters.length; formatterIdx++) { 62 Date parsed=parse(str,formatterIdx); 63 if(parsed!=null) { 64 return parsed; 65 } 66 } 67 return null; 68 } 69 70 protected Date parse(String str,int formatterIdx) { 71 if(formatterIdx>=0) { 72 ParsePosition pos=new ParsePosition (0); 73 if (str == null) { 74 return null; 75 } 76 Date 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 (millisecs); 85 } 86 catch(NumberFormatException exc) { 87 } 88 return null; 89 } 90 91 96 protected String format(Date date) { 97 return format(date,DEFAULT_FORMATTER_INDEX); 98 } 99 100 protected String format(Date 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 121 protected DateFormat getDateFormat(int index) { 122 if (index < 0 || index >= formatters.length) { 123 throw new IllegalArgumentException ("'index' [" + index + "] is out of bounds."); } 125 126 return formatters[index]; 127 } 128 } | Popular Tags |