1 16 17 package org.jboss.logging.jdk.format; 18 19 import java.util.TimeZone ; 20 import java.util.Date ; 21 import java.util.Calendar ; 22 import java.text.FieldPosition ; 23 import java.text.ParsePosition ; 24 25 27 41 public class ISO8601DateFormat extends AbsoluteTimeDateFormat 42 { 43 44 public ISO8601DateFormat() 45 { 46 } 47 48 public ISO8601DateFormat(TimeZone timeZone) 49 { 50 super(timeZone); 51 } 52 53 static private long lastTime; 54 static private char[] lastTimeString = new char[20]; 55 56 62 public StringBuffer format(Date date, StringBuffer sbuf, 63 FieldPosition fieldPosition) 64 { 65 66 long now = date.getTime(); 67 int millis = (int) (now % 1000); 68 69 if ((now - millis) != lastTime) 70 { 71 75 calendar.setTime(date); 76 77 int start = sbuf.length(); 78 79 int year = calendar.get(Calendar.YEAR); 80 sbuf.append(year); 81 82 String month; 83 switch (calendar.get(Calendar.MONTH)) 84 { 85 case Calendar.JANUARY: 86 month = "-01-"; 87 break; 88 case Calendar.FEBRUARY: 89 month = "-02-"; 90 break; 91 case Calendar.MARCH: 92 month = "-03-"; 93 break; 94 case Calendar.APRIL: 95 month = "-04-"; 96 break; 97 case Calendar.MAY: 98 month = "-05-"; 99 break; 100 case Calendar.JUNE: 101 month = "-06-"; 102 break; 103 case Calendar.JULY: 104 month = "-07-"; 105 break; 106 case Calendar.AUGUST: 107 month = "-08-"; 108 break; 109 case Calendar.SEPTEMBER: 110 month = "-09-"; 111 break; 112 case Calendar.OCTOBER: 113 month = "-10-"; 114 break; 115 case Calendar.NOVEMBER: 116 month = "-11-"; 117 break; 118 case Calendar.DECEMBER: 119 month = "-12-"; 120 break; 121 default: 122 month = "-NA-"; 123 break; 124 } 125 sbuf.append(month); 126 127 int day = calendar.get(Calendar.DAY_OF_MONTH); 128 if (day < 10) 129 sbuf.append('0'); 130 sbuf.append(day); 131 132 sbuf.append(' '); 133 134 int hour = calendar.get(Calendar.HOUR_OF_DAY); 135 if (hour < 10) 136 { 137 sbuf.append('0'); 138 } 139 sbuf.append(hour); 140 sbuf.append(':'); 141 142 int mins = calendar.get(Calendar.MINUTE); 143 if (mins < 10) 144 { 145 sbuf.append('0'); 146 } 147 sbuf.append(mins); 148 sbuf.append(':'); 149 150 int secs = calendar.get(Calendar.SECOND); 151 if (secs < 10) 152 { 153 sbuf.append('0'); 154 } 155 sbuf.append(secs); 156 157 sbuf.append(','); 158 159 sbuf.getChars(start, sbuf.length(), lastTimeString, 0); 161 lastTime = now - millis; 162 } 163 else 164 { 165 sbuf.append(lastTimeString); 166 } 167 168 169 if (millis < 100) 170 sbuf.append('0'); 171 if (millis < 10) 172 sbuf.append('0'); 173 174 sbuf.append(millis); 175 return sbuf; 176 } 177 178 181 public Date parse(java.lang.String s, ParsePosition pos) 182 { 183 return null; 184 } 185 } 186 187 | Popular Tags |