1 11 package org.eclipse.team.internal.ccvs.core.util; 12 13 14 import java.text.ParseException ; 15 import java.text.SimpleDateFormat ; 16 import java.util.Date ; 17 import java.util.Locale ; 18 import java.util.TimeZone ; 19 20 29 public class CVSDateFormatter { 30 31 private static final String ENTRYLINE_FORMAT = "E MMM dd HH:mm:ss yyyy"; private static final String SERVER_FORMAT = "dd MMM yyyy HH:mm:ss"; private static final int ENTRYLINE_TENS_DAY_OFFSET = 8; 34 35 private static final SimpleDateFormat serverFormat = new SimpleDateFormat (SERVER_FORMAT, Locale.US); 36 private static SimpleDateFormat entryLineFormat = new SimpleDateFormat (ENTRYLINE_FORMAT, Locale.US); 37 38 static { 39 entryLineFormat.setTimeZone(TimeZone.getTimeZone("GMT")); } 41 static synchronized public Date serverStampToDate(String text) throws ParseException { 42 serverFormat.setTimeZone(getTimeZone(text)); 43 Date date = serverFormat.parse(text); 44 return date; 45 } 46 47 static synchronized public Date entryLineToDate(String text) throws ParseException { 48 try { 49 if (text.charAt(ENTRYLINE_TENS_DAY_OFFSET) == ' ') { 50 StringBuffer buf = new StringBuffer (text); 51 buf.setCharAt(ENTRYLINE_TENS_DAY_OFFSET, '0'); 52 text = buf.toString(); 53 } 54 } catch (StringIndexOutOfBoundsException e) { 55 throw new ParseException (e.getMessage(), ENTRYLINE_TENS_DAY_OFFSET); 56 } 57 return entryLineFormat.parse(text); 58 } 59 60 static synchronized public String dateToEntryLine(Date date) { 61 if (date == null) return ""; String passOne = entryLineFormat.format(date); 63 if (passOne.charAt(ENTRYLINE_TENS_DAY_OFFSET) != '0') return passOne; 64 StringBuffer passTwo = new StringBuffer (passOne); 65 passTwo.setCharAt(ENTRYLINE_TENS_DAY_OFFSET, ' '); 66 return passTwo.toString(); 67 } 68 69 static synchronized public String dateToNotifyServer(Date date) { 70 serverFormat.setTimeZone(TimeZone.getTimeZone("GMT")); return serverFormat.format(date) + " GMT"; } 73 74 79 static private TimeZone getTimeZone(String dateFromServer) { 80 if (dateFromServer.lastIndexOf("0000") != -1) return TimeZone.getTimeZone("GMT"); String tz = null; 83 StringBuffer resultTz = new StringBuffer ("GMT"); if (dateFromServer.indexOf("-") != -1) { resultTz.append("-"); tz = dateFromServer.substring(dateFromServer.indexOf("-")); } else if (dateFromServer.indexOf("+") != -1) { resultTz.append('+'); 89 tz = dateFromServer.substring(dateFromServer.indexOf("+")); } 91 try { 92 if(tz!=null) { 93 resultTz.append(tz.substring(1, 3) + ":" + tz.substring(3, 5) ); return TimeZone.getTimeZone(resultTz.toString()); 95 } 96 } catch(IndexOutOfBoundsException e) { 97 return TimeZone.getTimeZone("GMT"); } 99 return TimeZone.getTimeZone("GMT"); } 101 } 102 | Popular Tags |