1 16 17 package org.apache.log4j.helpers; 18 19 import java.util.Date ; 20 import java.util.Calendar ; 21 import java.util.TimeZone ; 22 import java.text.FieldPosition ; 23 import java.text.ParsePosition ; 24 import java.text.DateFormat ; 25 26 27 36 public class AbsoluteTimeDateFormat extends DateFormat { 37 38 42 public final static String ABS_TIME_DATE_FORMAT = "ABSOLUTE"; 43 44 49 public final static String DATE_AND_TIME_DATE_FORMAT = "DATE"; 50 51 56 public final static String ISO8601_DATE_FORMAT = "ISO8601"; 57 58 public 59 AbsoluteTimeDateFormat() { 60 setCalendar(Calendar.getInstance()); 61 } 62 63 public 64 AbsoluteTimeDateFormat(TimeZone timeZone) { 65 setCalendar(Calendar.getInstance(timeZone)); 66 } 67 68 private static long previousTime; 69 private static char[] previousTimeWithoutMillis = new char[9]; 71 79 public 80 StringBuffer format(Date date, StringBuffer sbuf, 81 FieldPosition fieldPosition) { 82 83 long now = date.getTime(); 84 int millis = (int)(now % 1000); 85 86 if ((now - millis) != previousTime) { 87 91 calendar.setTime(date); 92 93 int start = sbuf.length(); 94 95 int hour = calendar.get(Calendar.HOUR_OF_DAY); 96 if(hour < 10) { 97 sbuf.append('0'); 98 } 99 sbuf.append(hour); 100 sbuf.append(':'); 101 102 int mins = calendar.get(Calendar.MINUTE); 103 if(mins < 10) { 104 sbuf.append('0'); 105 } 106 sbuf.append(mins); 107 sbuf.append(':'); 108 109 int secs = calendar.get(Calendar.SECOND); 110 if(secs < 10) { 111 sbuf.append('0'); 112 } 113 sbuf.append(secs); 114 sbuf.append(','); 115 116 sbuf.getChars(start, sbuf.length(), previousTimeWithoutMillis, 0); 118 119 previousTime = now - millis; 120 } 121 else { 122 sbuf.append(previousTimeWithoutMillis); 123 } 124 125 126 127 if(millis < 100) 128 sbuf.append('0'); 129 if(millis < 10) 130 sbuf.append('0'); 131 132 sbuf.append(millis); 133 return sbuf; 134 } 135 136 139 public 140 Date parse(String s, ParsePosition pos) { 141 return null; 142 } 143 } 144 | Popular Tags |