KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > ftp > FTPDate


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.ftp;
18
19 import java.util.*;
20
21 /**
22  * FTP Date Utility Class
23  *
24  * @author GKSpencer
25  */

26 public class FTPDate
27 {
28
29     // Constants
30
//
31
// Six months in ticks
32

33     protected final static long SIX_MONTHS = 183L * 24L * 60L * 60L * 1000L;
34
35     // Month names
36

37     protected final static String JavaDoc[] _months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
38             "Nov", "Dec" };
39
40     /**
41      * Pack a date string in Unix format The format is 'Mmm dd hh:mm' if the file is less than six
42      * months old, else the format is 'Mmm dd yyyy'.
43      *
44      * @param buf StringBuffer
45      * @param dt Date
46      */

47     public final static void packUnixDate(StringBuffer JavaDoc buf, Date dt)
48     {
49
50         // Check if the date is valid
51

52         if (dt == null)
53         {
54             buf.append("------------");
55             return;
56         }
57
58         // Get the time raw value
59

60         long timeVal = dt.getTime();
61         if (timeVal < 0)
62         {
63             buf.append("------------");
64             return;
65         }
66
67         // Add the month name and date parts to the string
68

69         Calendar cal = new GregorianCalendar();
70         cal.setTime(dt);
71         buf.append(_months[cal.get(Calendar.MONTH)]);
72         buf.append(" ");
73
74         int dayOfMonth = cal.get(Calendar.DATE);
75         if (dayOfMonth < 10)
76             buf.append(" ");
77         buf.append(dayOfMonth);
78         buf.append(" ");
79
80         // If the file is less than six months old we append the file time, else we append the year
81

82         long timeNow = System.currentTimeMillis();
83         if (Math.abs(timeNow - timeVal) > SIX_MONTHS)
84         {
85
86             // Append the year
87

88             buf.append(cal.get(Calendar.YEAR));
89         }
90         else
91         {
92
93             // Append the file time as hh:mm
94

95             int hr = cal.get(Calendar.HOUR_OF_DAY);
96             if (hr < 10)
97                 buf.append("0");
98             buf.append(hr);
99             buf.append(":");
100
101             int sec = cal.get(Calendar.SECOND);
102             if (sec < 10)
103                 buf.append("0");
104             buf.append(sec);
105         }
106     }
107 }
108
Popular Tags