1 26 27 package it.stefanochizzolini.clown.objects; 28 29 import it.stefanochizzolini.clown.files.File; 30 import java.text.SimpleDateFormat ; 31 import java.util.Date ; 32 33 36 public class PdfDate 37 extends PdfAtomicObject<Date > 38 implements IPdfString 39 { 40 49 protected static final SimpleDateFormat formatter; 53 55 static 57 {formatter = new SimpleDateFormat ("yyyyMMddHHmmssZ");} 58 60 65 public static String toPdf( 66 Date value 67 ) 68 {return PdfLiteral.toPdf(format(value));} 69 70 73 public static Date toDate( 74 String value 75 ) 76 { 77 value = value.substring(2); 81 int length = value.length(); 82 switch(length) 83 { 84 case 8: value += "000000+0000"; 86 break; 87 case 14: value += "+0000"; 89 break; 90 case 15: value = value.substring(0,length-1) + "+0000"; 92 break; 93 case 17: value += "00"; 95 break; 96 case 21: value = value.substring(0,length-1).replace("\'",""); 98 break; 99 } 100 101 try 102 { 103 return formatter.parse(value); 105 } 106 catch(Exception e) 107 { 108 throw new RuntimeException (e); 110 } 111 } 112 114 118 protected static String format( 119 Date value 120 ) 121 { 122 String formattedValue = formatter.format(value); 123 124 return ("D:" + formattedValue.substring(0,17) + '\'' + formattedValue.substring(17) + '\''); 125 } 126 130 public PdfDate( 133 ) 134 {} 135 136 public PdfDate( 137 Date value 138 ) 139 {setValue(value);} 140 142 @Override 145 public Object clone( 146 File context 147 ) 148 { 149 PdfDate clone = (PdfDate)super.clone(); 151 152 clone.setValue((Date )getValue().clone()); 155 return clone; 156 } 157 158 @Override 159 public String toPdf( 160 ) 161 {return PdfDate.toPdf(getValue());} 162 163 public String getStringValue( 165 ) 166 {return format(getValue());} 167 168 public void setStringValue( 169 String value 170 ) 171 {setValue(toDate(value));} 172 } | Popular Tags |