1import java.util.*;
 2/**
 3 * <p>Title: Time  </p>
 4 * <p>Description: </p>
 5 *      此类主要用来取得本地系统的系统时间并用下面5种格式显示
 6 *              1. YYMMDDHH         8位
 7 *              2. YYMMDDHHmm       10位
 8 *              3. YYMMDDHHmmss     12位
 9 *              4. YYYYMMDDHHmmss   14位
10 *              5. YYMMDDHHmmssxxx  15位 (最后的xxx 是毫秒)
11 * <p>Copyright: Copyright (c) 2003</p>
12 * <p>Company: hoten </p>
13 * @author lqf
14 * @version 1.0
15 */

16public class CTime {
17    public static final int YYMMDDhhmmssxxx=15;
18    public static final int YYYYMMDDhhmmss=14;
19    public static final int YYMMDDhhmmss=12;
20    public static final int YYMMDDhhmm=10;
21    public static final int YYMMDDhh=8;
22/**
23 * 取得本地系统的时间,时间格式由参数决定
24 * @param format 时间格式由常量决定
25 * @return String 具有format格式的字符串
26 */

27    public synchronized static String  getTime(int format){
28        StringBuffer cTime=new StringBuffer(10);
29        Calendar time=Calendar.getInstance();
30        int miltime=time.get(Calendar.MILLISECOND);
31        int second=time.get(Calendar.SECOND);
32        int minute=time.get(Calendar.MINUTE);
33        int hour=time.get(Calendar.HOUR_OF_DAY);
34        int day =time.get(Calendar.DAY_OF_MONTH);
35        int month=time.get(Calendar.MONTH)+1;
36        int year =time.get(Calendar.YEAR);
37        if(format!=14){
38            if(year>=2000) year=year-2000;
39            else year=year-1900;
40        }

41        if(format>=2){
42            if(format==14) cTime.append(year);
43            else    cTime.append(getFormatTime(year,2));
44        }

45        if(format>=4)
46            cTime.append(getFormatTime(month,2));
47        if(format>=6)
48            cTime.append(getFormatTime(day,2));
49        if(format>=8)
50            cTime.append(getFormatTime(hour,2));
51        if(format>=10)
52            cTime.append(getFormatTime(minute,2));
53        if(format>=12)
54            cTime.append(getFormatTime(second,2));
55        if(format>=15)
56            cTime.append(getFormatTime(miltime,3));
57        return cTime.toString();
58    }

59/**
60 * 产生任意位的字符串
61 * @param time 要转换格式的时间
62 * @param format 转换的格式
63 * @return String 转换的时间
64 */

65    private synchronized static String getFormatTime(int time,int format){
66        StringBuffer numm=new StringBuffer();
67        int length=String.valueOf(time).length();
68
69        if(format<length) return null;
70
71        for(int i=0 ;i<format-length ;i++){
72            numm.append("0");
73        }

74        numm.append(time);
75        return numm.toString().trim();
76    }

77}