1 36 package org.columba.ristretto.message; 37 38 import java.util.Calendar ; 39 import java.util.Date ; 40 import java.util.TimeZone ; 41 42 43 49 public class MessageDate { 50 51 private static final String [] dayOfWeek = { 52 "Sun", 53 "Mon", 54 "Tue", 55 "Wed", 56 "Thu", 57 "Fri", 58 "Sat" 59 }; 60 61 private static final String [] month = { 62 "Jan", 63 "Feb", 64 "Mar", 65 "Apr", 66 "May", 67 "Jun", 68 "Jul", 69 "Aug", 70 "Sep", 71 "Oct", 72 "Nov", 73 "Dec" 74 }; 75 76 82 public static final String toString( Date date) { 83 return toString( date, TimeZone.getDefault() ); 84 } 85 86 93 public static final String toString( Date date, TimeZone tz ) { 94 Calendar calendar = Calendar.getInstance(tz); 95 calendar.setTime( date ); 96 97 StringBuffer result = new StringBuffer (31); 98 99 result.append( dayOfWeek[calendar.get(Calendar.DAY_OF_WEEK)-1]); 101 result.append(", "); 102 103 result.append( calendar.get(Calendar.DAY_OF_MONTH)); 105 result.append(' '); 106 107 result.append( month[calendar.get(Calendar.MONTH)]); 109 result.append(' '); 110 111 result.append( calendar.get(Calendar.YEAR)); 113 result.append(' '); 114 115 int hour = calendar.get(Calendar.HOUR_OF_DAY); 117 if (hour < 10) result.append('0'); 118 result.append( hour); 119 result.append(':'); 120 121 int min = calendar.get(Calendar.MINUTE); 123 if( min < 10 ) result.append( '0' ); 124 result.append( min ); 125 result.append(':'); 126 127 int sec = calendar.get(Calendar.SECOND); 129 if( sec < 10 ) result.append( '0' ); 130 result.append(sec); 131 result.append(' '); 132 133 int rawOffset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)); 135 if( rawOffset < 0 ) { 136 int hours = (-rawOffset) / 3600000; 137 int minutes = ((-rawOffset) % 3600000) / 60000; 138 139 result.append( "-"); 140 if( hours < 10 ) { 141 result.append('0'); 142 } 143 result.append( hours); 144 145 if( minutes < 10 ) { 146 result.append('0'); 147 } 148 result.append(minutes); 149 } else { 150 int hours = rawOffset / 3600000; 151 int minutes = (rawOffset % 3600000) / 60000; 152 153 result.append( "+" ); 154 if( hours < 10 ) { 155 result.append('0'); 156 } 157 result.append( hours); 158 159 if( minutes < 10 ) { 160 result.append('0'); 161 } 162 result.append(minutes); 163 } 164 165 return result.toString(); 166 } 167 168 } 169 | Popular Tags |