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