1 31 package org.pdfbox.util; 32 33 import java.text.ParseException ; 34 import java.text.SimpleDateFormat ; 35 36 import java.io.IOException ; 37 38 import java.util.Calendar ; 39 import java.util.Date ; 40 import java.util.GregorianCalendar ; 41 import java.util.SimpleTimeZone ; 42 import java.util.TimeZone ; 43 44 import org.pdfbox.cos.COSString; 45 46 53 public class DateConverter 54 { 55 private static final SimpleDateFormat PDF_DATE_FORMAT = new SimpleDateFormat ( "yyyyMMddHHmmss" ); 56 57 private static final SimpleDateFormat [] POTENTIAL_FORMATS = new SimpleDateFormat [] { 61 new SimpleDateFormat ("EEEE, dd MMM yyyy hh:mm:ss a"), 62 new SimpleDateFormat ("EEEE, MMM dd, yyyy hh:mm:ss a"), 63 new SimpleDateFormat ("MM/dd/yyyy hh:mm:ss"), 64 new SimpleDateFormat ("MM/dd/yyyy")}; 65 66 private static final SimpleDateFormat ISO_8601_DATE_FORMAT = new SimpleDateFormat ( "yyyy-MM-dd'T'HH:mm:ss" ); 67 68 private DateConverter() 69 { 70 } 72 73 80 public static String toString( Calendar date ) 81 { 82 String retval = null; 83 if( date != null ) 84 { 85 StringBuffer buffer = new StringBuffer (); 86 TimeZone zone = date.getTimeZone(); 87 long offsetInMinutes = zone.getOffset( date.getTimeInMillis() )/1000/60; 88 long hours = Math.abs( offsetInMinutes/60 ); 89 long minutes = Math.abs( offsetInMinutes%60 ); 90 buffer.append( "D:" ); 91 buffer.append( PDF_DATE_FORMAT.format( date.getTime() ) ); 92 if( offsetInMinutes == 0 ) 93 { 94 buffer.append( "Z" ); 95 } 96 else if( offsetInMinutes < 0 ) 97 { 98 buffer.append( "-" ); 99 } 100 else 101 { 102 buffer.append( "+" ); 103 } 104 if( hours < 10 ) 105 { 106 buffer.append( "0" ); 107 } 108 buffer.append( hours ); 109 buffer.append( "'" ); 110 if( minutes < 10 ) 111 { 112 buffer.append( "0" ); 113 } 114 buffer.append( minutes ); 115 buffer.append( "'" ); 116 retval = buffer.toString(); 117 118 } 119 return retval; 120 } 121 122 131 public static Calendar toCalendar( COSString date ) throws IOException 132 { 133 Calendar retval = null; 134 if( date != null ) 135 { 136 retval = toCalendar( date.getString() ); 137 } 138 139 return retval; 140 } 141 142 151 public static Calendar toCalendar( String date ) throws IOException 152 { 153 Calendar retval = null; 154 if( date != null && date.trim().length() > 0 ) 155 { 156 int year = 0; 158 int month = 1; 159 int day = 1; 160 int hour = 0; 161 int minute = 0; 162 int second = 0; 163 try 165 { 166 SimpleTimeZone zone = null; 167 if( date.startsWith( "D:" ) ) 168 { 169 date = date.substring( 2, date.length() ); 170 } 171 if( date.length() < 4 ) 172 { 173 throw new IOException ( "Error: Invalid date format '" + date + "'" ); 174 } 175 year = Integer.parseInt( date.substring( 0, 4 ) ); 176 if( date.length() >= 6 ) 177 { 178 month = Integer.parseInt( date.substring( 4, 6 ) ); 179 } 180 if( date.length() >= 8 ) 181 { 182 day = Integer.parseInt( date.substring( 6, 8 ) ); 183 } 184 if( date.length() >= 10 ) 185 { 186 hour = Integer.parseInt( date.substring( 8, 10 ) ); 187 } 188 if( date.length() >= 12 ) 189 { 190 minute = Integer.parseInt( date.substring( 10, 12 ) ); 191 } 192 if( date.length() >= 14 ) 193 { 194 second = Integer.parseInt( date.substring( 12, 14 ) ); 195 } 196 retval = new GregorianCalendar ( year, month-1, day, hour, minute, second ); 197 if( date.length() >= 15 ) 198 { 199 char sign = date.charAt( 14 ); 200 if( sign == 'Z' ) 201 { 202 zone = new SimpleTimeZone (0,"Unknown"); 203 } 204 else 205 { 206 int hours = 0; 207 int minutes = 0; 208 if( date.length() >= 17 ) 209 { 210 if( sign == '+' ) 211 { 212 hours = Integer.parseInt( date.substring( 15, 17 ) ); 214 } 215 else 216 { 217 hours = Integer.parseInt( date.substring( 14, 17 ) ); 218 } 219 } 220 if( date.length() > 20 ) 221 { 222 minutes = Integer.parseInt( date.substring( 18, 20 ) ); 223 } 224 zone = new SimpleTimeZone ( hours*60*60*1000 + minutes*60*1000, "Unknown" ); 225 } 226 retval.setTimeZone( zone ); 227 } 228 } 229 catch( NumberFormatException e ) 230 { 231 for( int i=0; retval == null && i<POTENTIAL_FORMATS.length; i++ ) 232 { 233 try 234 { 235 Date utilDate = POTENTIAL_FORMATS[i].parse( date ); 236 retval = new GregorianCalendar (); 237 retval.setTime( utilDate ); 238 } 239 catch( ParseException pe ) 240 { 241 } 243 } 244 if( retval == null ) 245 { 246 throw new IOException ( "Error converting date:" + date ); 248 } 249 } 250 } 251 return retval; 252 } 253 254 260 public static String toISO8601( Calendar cal ) 261 { 262 StringBuffer retval = new StringBuffer (); 263 retval.append( ISO_8601_DATE_FORMAT.format( cal.getTime() ) ); 264 int timeZone = cal.get( Calendar.ZONE_OFFSET ); 265 if( timeZone < 0 ) 266 { 267 retval.append( "-" ); 268 } 269 else 270 { 271 retval.append( "+" ); 272 } 273 timeZone = Math.abs( timeZone ); 274 int hours = timeZone/1000/60/60; 276 int minutes = (timeZone - (hours*1000*60*60))/1000/1000; 277 if( hours < 10 ) 278 { 279 retval.append( "0" ); 280 } 281 retval.append( Integer.toString( hours ) ); 282 retval.append( ":" ); 283 if( minutes < 10 ) 284 { 285 retval.append( "0" ); 286 } 287 retval.append( Integer.toString( minutes ) ); 288 289 return retval.toString(); 290 } 291 } | Popular Tags |