1 package com.calipso.common; 2 3 import com.calipso.reportgenerator.common.InfoException; 4 5 import java.util.*; 6 import java.text.*; 7 import java.io.Serializable ; 8 import com.calipso.reportgenerator.reportcalculator.SharedDate; 9 import com.calipso.reportgenerator.common.LanguageTraslator; 10 11 14 public class DateEx implements Serializable , Comparable { 15 private static final List possiblePatterns = new ArrayList(Arrays.asList(new Object [] {"yyyyMMdd", "yyyyMMddHHmmssSSS"})); 17 18 private Date date; 19 20 public DateEx(Object object) throws InfoException{ 21 setDateFromObject(object, ""); 22 } 23 24 private void setDateFromObject(Object date, String inputFormat) throws InfoException{ 25 Date newDate = null; 26 if(date instanceof Date){ 27 newDate = (Date)date; 28 }else if(date instanceof String ){ 29 String localDate = ((String )date).trim(); 30 newDate = getDateFromString(localDate, inputFormat); 31 }else{ 32 String localDate = date.toString(); 33 newDate = getDateFromString(localDate, inputFormat); 34 } 35 if(newDate != null){ 36 this.date = newDate; 37 }else{ 38 throw new InfoException(LanguageTraslator.traslate("77")); 39 } 40 } 41 42 public DateEx(Date date) { 43 this.date = date; 44 } 45 46 public DateEx(Object date, String inputFormat) throws InfoException { 47 setDateFromObject(date, inputFormat); 48 } 49 50 60 public DateEx(Number date, String inputFormat) throws InfoException{ 61 Format numberFormat = new DecimalFormat("#####################"); 62 String value = numberFormat.format(date); 63 try{ 64 setDateFromObject(value, inputFormat); 65 }catch (InfoException e){ 66 DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); 67 Date expiration; 68 try{ 69 expiration = dateFormat.parse("18991230"); 70 }catch (ParseException e1){ 71 throw new InfoException(LanguageTraslator.traslate("77"), e1); 72 } 73 GregorianCalendar gregorianCalendar = new GregorianCalendar(); 74 gregorianCalendar.setTime(expiration); 75 gregorianCalendar.add(GregorianCalendar.DAY_OF_YEAR,(new Integer (value)).intValue()); 76 this.date = gregorianCalendar.getTime(); 77 } 78 } 79 80 89 private Date getDateFromString(String localDate, String inputFormat) { 90 Date result = null; 91 if(inputFormat!=null && !inputFormat.equalsIgnoreCase("")){ 92 setFirst(possiblePatterns, inputFormat); 93 } 94 for (Iterator iterator = possiblePatterns.iterator(); result==null && iterator.hasNext();) { 95 String pattern = (String )iterator.next(); 96 DateFormat format = new SimpleDateFormat(pattern); 97 format.setLenient(false); 98 try { 99 result = format.parse(localDate); 100 } catch (ParseException e) { 101 } 103 } 104 if(result==null){ 105 DateFormat format = SimpleDateFormat.getDateInstance(DateFormat.SHORT, LanguageTraslator.getLocale()); 106 try { 107 format.parse(localDate); 108 } catch (ParseException e) { 109 } 111 } 112 return result; 113 } 114 115 private void setFirst(List posiblepatterns, String inputFormat) { 116 if(posiblepatterns.contains(inputFormat)){ 117 posiblepatterns.remove(inputFormat); 118 } 119 Object tmp = posiblepatterns.get(0); 120 if(!inputFormat.equals(tmp)){ 121 posiblepatterns.set(0, inputFormat); 122 posiblepatterns.add(tmp); 123 } 124 } 125 126 public String toString() { 127 SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.SHORT, LanguageTraslator.getLocale()); return new String (dateFormat.format((date))); 129 } 130 131 public Date getDate() { 132 return date; 133 } 134 135 public int compareTo(Object o) { 136 if (o instanceof DateEx) { 137 return date.compareTo(((DateEx) o).getDate()); 138 } 139 else { 140 if (o instanceof SharedDate) { 141 return date.compareTo(((SharedDate) o).getDateEx().getDate()); 142 } 143 else { 144 if (o instanceof Date) { 145 return date.compareTo(((Date) o)); 146 } 147 else { 148 if (o instanceof String ) { 149 try { 153 DateEx localDate = new DateEx(o, ""); 154 return date.compareTo(localDate.getDate()); 156 } 157 catch (Exception e) { 158 return -1; 159 } 160 } 161 } 162 } 163 } 164 return -1; 165 } 166 167 public boolean equals(Object o){ 168 return ((DateEx)o).getDate().equals(date); 169 } 170 171 180 181 } | Popular Tags |