KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nwalsh > saxon > CVS


1 package com.nwalsh.saxon;
2
3 import java.io.*;
4 import java.util.Calendar JavaDoc;
5 import java.util.GregorianCalendar JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.Locale JavaDoc;
8 import java.util.TimeZone JavaDoc;
9 import java.text.DateFormat JavaDoc;
10 import java.text.ParseException JavaDoc;
11
12 /**
13  * <p>Saxon extension to convert CVS date strings into local time</p>
14  *
15  * <p>$Id: CVS.java,v 1.1 2001/07/16 21:23:57 nwalsh Exp $</p>
16  *
17  * <p>Copyright (C) 2000 Norman Walsh.</p>
18  *
19  * <p>This class provides a
20  * <a HREF="http://users.iclway.co.uk/mhkay/saxon/">Saxon</a>
21  * extension to turn the CVS date strings, which are UTC:</p>
22  *
23  * <pre>&#36;Date: 2000/11/09 02:34:20 &#36;</pre>
24  *
25  * <p>into legibly formatted local time:</p>
26  *
27  * <pre>Wed Nov 08 18:34:20 PST 2000</pre>
28  *
29  * <p>(I happened to be in California when I wrote this documentation.)</p>
30
31  * <p><b>Change Log:</b></p>
32  * <dl>
33  * <dt>1.0</dt>
34  * <dd><p>Initial release.</p></dd>
35  * </dl>
36  *
37  * @author Norman Walsh
38  * <a HREF="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
39  *
40  * @version $Id: CVS.java,v 1.1 2001/07/16 21:23:57 nwalsh Exp $
41  *
42  */

43 public class CVS {
44   /**
45    * <p>Constructor for CVS</p>
46    *
47    * <p>All of the methods are static, so the constructor does nothing.</p>
48    */

49   public CVS() {
50   }
51
52   /**
53    * <p>Convert a CVS date string into local time.</p>
54    *
55    * @param cvsDate The CVS date string.
56    *
57    * @return The date, converted to local time and reformatted.
58    */

59   public static String JavaDoc localTime (String JavaDoc cvsDate) {
60     // A cvsDate has the following form "$Date: 2001/07/16 21:23:57 $"
61
if (!cvsDate.startsWith("$Date: ")) {
62       return cvsDate;
63     }
64
65     String JavaDoc yrS = cvsDate.substring(7,11);
66     String JavaDoc moS = cvsDate.substring(12,14);
67     String JavaDoc daS = cvsDate.substring(15,17);
68     String JavaDoc hrS = cvsDate.substring(18,20);
69     String JavaDoc miS = cvsDate.substring(21,23);
70     String JavaDoc seS = cvsDate.substring(24,26);
71
72     TimeZone JavaDoc tz = TimeZone.getTimeZone("GMT+0");
73     GregorianCalendar JavaDoc gmtCal = new GregorianCalendar JavaDoc(tz);
74
75     try {
76       gmtCal.set(Integer.parseInt(yrS),
77          Integer.parseInt(moS)-1,
78          Integer.parseInt(daS),
79          Integer.parseInt(hrS),
80          Integer.parseInt(miS),
81          Integer.parseInt(seS));
82     } catch (NumberFormatException JavaDoc e) {
83       // nop
84
}
85
86     Date JavaDoc d = gmtCal.getTime();
87
88     return d.toString();
89   }
90 }
91
Popular Tags