1 12 package org.eclipse.jface.internal.databinding.provisional.conversion; 13 14 import com.ibm.icu.text.DateFormat; 15 import java.text.ParsePosition ; 16 import com.ibm.icu.text.SimpleDateFormat; 17 import java.util.Date ; 18 19 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 37 private DateFormat[] formatters={ 39 new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS Z"), new SimpleDateFormat("HH:mm:ss.SSS"), 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 56 protected Date parse(String str) { 57 for (int formatterIdx = 0; formatterIdx < formatters.length; formatterIdx++) { 58 Date parsed=parse(str,formatterIdx); 59 if(parsed!=null) { 60 return parsed; 61 } 62 } 63 return null; 64 } 65 66 protected Date parse(String str,int formatterIdx) { 67 if(formatterIdx>=0) { 68 ParsePosition pos=new ParsePosition (0); 69 Date 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 (millisecs); 78 } 79 catch(NumberFormatException exc) { 80 } 81 return null; 82 } 83 84 87 protected String format(Date date) { 88 return format(date,DEFAULT_FORMATTER_INDEX); 89 } 90 91 protected String format(Date 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 |