1 17 package org.alfresco.util; 18 19 import java.util.Calendar ; 20 import java.util.Date ; 21 import java.util.GregorianCalendar ; 22 import java.util.TimeZone ; 23 24 25 44 public class ISO8601DateFormat 45 { 46 47 53 public static String format(Date isoDate) 54 { 55 Calendar calendar = new GregorianCalendar (); 56 calendar.setTime(isoDate); 57 58 StringBuffer formatted = new StringBuffer (); 59 padInt(formatted, calendar.get(Calendar.YEAR), 4); 60 formatted.append('-'); 61 padInt(formatted, calendar.get(Calendar.MONTH) + 1, 2); 62 formatted.append('-'); 63 padInt(formatted, calendar.get(Calendar.DAY_OF_MONTH), 2); 64 formatted.append('T'); 65 padInt(formatted, calendar.get(Calendar.HOUR_OF_DAY), 2); 66 formatted.append(':'); 67 padInt(formatted, calendar.get(Calendar.MINUTE), 2); 68 formatted.append(':'); 69 padInt(formatted, calendar.get(Calendar.SECOND), 2); 70 formatted.append('.'); 71 padInt(formatted, calendar.get(Calendar.MILLISECOND), 3); 72 73 TimeZone tz = calendar.getTimeZone(); 74 int offset = tz.getOffset(calendar.getTimeInMillis()); 75 if (offset != 0) 76 { 77 int hours = Math.abs((offset / (60 * 1000)) / 60); 78 int minutes = Math.abs((offset / (60 * 1000)) % 60); 79 formatted.append(offset < 0 ? '-' : '+'); 80 padInt(formatted, hours, 2); 81 formatted.append(':'); 82 padInt(formatted, minutes, 2); 83 } 84 else 85 { 86 formatted.append('Z'); 87 } 88 89 return formatted.toString(); 90 } 91 92 93 99 public static Date parse(String isoDate) 100 { 101 Date parsed = null; 102 103 try 104 { 105 int offset = 0; 106 107 int year = Integer.parseInt(isoDate.substring(offset, offset += 4)); 109 if (isoDate.charAt(offset) != '-') 110 { 111 throw new IndexOutOfBoundsException ("Expected - character but found " + isoDate.charAt(offset)); 112 } 113 114 int month = Integer.parseInt(isoDate.substring(offset += 1, offset += 2)); 116 if (isoDate.charAt(offset) != '-') 117 { 118 throw new IndexOutOfBoundsException ("Expected - character but found " + isoDate.charAt(offset)); 119 } 120 121 int day = Integer.parseInt(isoDate.substring(offset += 1, offset += 2)); 123 if (isoDate.charAt(offset) != 'T') 124 { 125 throw new IndexOutOfBoundsException ("Expected T character but found " + isoDate.charAt(offset)); 126 } 127 128 int hour = Integer.parseInt(isoDate.substring(offset += 1, offset += 2)); 130 if (isoDate.charAt(offset) != ':') 131 { 132 throw new IndexOutOfBoundsException ("Expected T character but found " + isoDate.charAt(offset)); 133 } 134 int minutes = Integer.parseInt(isoDate.substring(offset += 1, offset += 2)); 135 if (isoDate.charAt(offset) != ':') 136 { 137 throw new IndexOutOfBoundsException ("Expected : character but found " + isoDate.charAt(offset)); 138 } 139 int seconds = Integer.parseInt(isoDate.substring(offset += 1 , offset += 2)); 140 if (isoDate.charAt(offset) != '.') 141 { 142 throw new IndexOutOfBoundsException ("Expected . character but found " + isoDate.charAt(offset)); 143 } 144 int milliseconds = Integer.parseInt(isoDate.substring(offset += 1, offset += 3)); 145 146 String timezoneId; 148 char timezoneIndicator = isoDate.charAt(offset); 149 if (timezoneIndicator == '+' || timezoneIndicator == '-') 150 { 151 timezoneId = "GMT" + isoDate.substring(offset); 152 } 153 else if (timezoneIndicator == 'Z') 154 { 155 timezoneId = "GMT"; 156 } 157 else 158 { 159 throw new IndexOutOfBoundsException ("Invalid time zone indicator " + timezoneIndicator); 160 } 161 TimeZone timezone = TimeZone.getTimeZone(timezoneId); 162 if (!timezone.getID().equals(timezoneId)) 163 { 164 throw new IndexOutOfBoundsException (); 165 } 166 167 Calendar calendar = Calendar.getInstance(timezone); 169 calendar.setLenient(false); 170 calendar.set(Calendar.YEAR, year); 171 calendar.set(Calendar.MONTH, month - 1); 172 calendar.set(Calendar.DAY_OF_MONTH, day); 173 calendar.set(Calendar.HOUR_OF_DAY, hour); 174 calendar.set(Calendar.MINUTE, minutes); 175 calendar.set(Calendar.SECOND, seconds); 176 calendar.set(Calendar.MILLISECOND, milliseconds); 177 178 parsed = calendar.getTime(); 180 } 181 catch(IndexOutOfBoundsException e) 182 { 183 } 184 catch(NumberFormatException e) 185 { 186 } 187 catch(IllegalArgumentException e) 188 { 189 } 190 191 return parsed; 192 } 193 194 197 private static void padInt(StringBuffer buffer, int value, int length) 198 { 199 String strValue = Integer.toString(value); 200 for (int i = length - strValue.length(); i > 0; i--) 201 { 202 buffer.append('0'); 203 } 204 buffer.append(strValue); 205 } 206 207 } 208 | Popular Tags |