1 50 51 package com.lowagie.text.pdf; 52 53 import java.text.SimpleDateFormat ; 54 import java.util.Calendar ; 55 import java.util.GregorianCalendar ; 56 import java.util.SimpleTimeZone ; 57 58 73 74 public class PdfDate extends PdfString { 75 76 private static final int DATE_SPACE[] = {Calendar.YEAR, 4, 0, Calendar.MONTH, 2, -1, Calendar.DAY_OF_MONTH, 2, 0, 77 Calendar.HOUR_OF_DAY, 2, 0, Calendar.MINUTE, 2, 0, Calendar.SECOND, 2, 0}; 78 79 81 86 87 public PdfDate(Calendar d) { 88 super(); 89 StringBuffer date = new StringBuffer ("D:"); 90 date.append(setLength(d.get(Calendar.YEAR), 4)); 91 date.append(setLength(d.get(Calendar.MONTH) + 1, 2)); 92 date.append(setLength(d.get(Calendar.DATE), 2)); 93 date.append(setLength(d.get(Calendar.HOUR_OF_DAY), 2)); 94 date.append(setLength(d.get(Calendar.MINUTE), 2)); 95 date.append(setLength(d.get(Calendar.SECOND), 2)); 96 int timezone = (d.get(Calendar.ZONE_OFFSET) + d.get(Calendar.DST_OFFSET)) / (60 * 60 * 1000); 97 if (timezone == 0) { 98 date.append('Z'); 99 } 100 else if (timezone < 0) { 101 date.append('-'); 102 timezone = -timezone; 103 } 104 else { 105 date.append('+'); 106 } 107 if (timezone != 0) { 108 date.append(setLength(timezone, 2)).append('\''); 109 int zone = Math.abs((d.get(Calendar.ZONE_OFFSET) + d.get(Calendar.DST_OFFSET)) / (60 * 1000)) - (timezone * 60); 110 date.append(setLength(zone, 2)).append('\''); 111 } 112 value = date.toString(); 113 } 114 115 118 119 public PdfDate() { 120 this(new GregorianCalendar ()); 121 } 122 123 131 132 private String setLength(int i, int length) { StringBuffer tmp = new StringBuffer (); 134 tmp.append(i); 135 while (tmp.length() < length) { 136 tmp.insert(0, "0"); 137 } 138 tmp.setLength(length); 139 return tmp.toString(); 140 } 141 142 146 public String getW3CDate() { 147 return getW3CDate(value); 148 } 149 150 155 public static String getW3CDate(String d) { 156 SimpleDateFormat w3c = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 157 Calendar c = decode(d); 158 return w3c.format(c.getTime()); 159 } 160 161 167 public static Calendar decode(String s) { 168 try { 169 if (s.startsWith("D:")) 170 s = s.substring(2); 171 GregorianCalendar calendar; 172 int slen = s.length(); 173 int idx = s.indexOf('Z'); 174 if (idx >= 0) { 175 slen = idx; 176 calendar = new GregorianCalendar (new SimpleTimeZone (0, "ZPDF")); 177 } 178 else { 179 int sign = 1; 180 idx = s.indexOf('+'); 181 if (idx < 0) { 182 idx = s.indexOf('-'); 183 if (idx >= 0) 184 sign = -1; 185 } 186 if (idx < 0) 187 calendar = new GregorianCalendar (); 188 else { 189 int offset = Integer.parseInt(s.substring(idx + 1, idx + 3)) * 60; 190 if (idx + 5 < s.length()) 191 offset += Integer.parseInt(s.substring(idx + 4, idx + 6)); 192 calendar = new GregorianCalendar (new SimpleTimeZone (offset * sign * 60000, "ZPDF")); 193 slen = idx; 194 } 195 } 196 calendar.clear(); 197 idx = 0; 198 for (int k = 0; k < DATE_SPACE.length; k += 3) { 199 if (idx >= slen) 200 break; 201 calendar.set(DATE_SPACE[k], Integer.parseInt(s.substring(idx, idx + DATE_SPACE[k + 1])) + DATE_SPACE[k + 2]); 202 idx += DATE_SPACE[k + 1]; 203 } 204 return calendar; 205 } 206 catch (Exception e) { 207 return null; 208 } 209 } 210 } | Popular Tags |