JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 民國年的DateFormat [Re:ACheng]
atticcat

There is no cookie



發文: 702
積分: 4
於 2005-11-16 18:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟剛好有這個需求~看了大大的東西有了些靈感.
所以就自己寫了一個...
程式沒有很聰明,都是偷吃步,而且效率可能勘慮..
不過還是希望對有需要的人有幫助.
(如果亂搞,同時pattern裏有民國年跟西元年的話,或是eeeee~parse會出錯,)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package util;
 
import java.util.*;
import java.text.*;
 
public class ROCDateTimeFormat extends SimpleDateFormat
{
    private static final long serialVersionUID = 1;
 
    protected String pattern;
 
    protected Vector patternSegment = new Vector();
 
    public ROCDateTimeFormat(String pattern) {
        super();
        this.pattern = pattern;
        segmentPattern();
    }
 
    private void segmentPattern() {
        int index1;
        int index2;
        int previousIndex = 0;
        while ((index1 = pattern.indexOf("'", previousIndex)) != -1) {
            index2 = pattern.indexOf("'", index1 + 1);
            if (index2 == -1) {
                throw new RuntimeException("Illegal Pattern:" + pattern);
            }
            String prePattern = pattern.substring(previousIndex, index1);
            
            String staticWord = pattern.substring(index1 + 1, index2);
            previousIndex = index2 + 1;
            patternSegment.addElement(prePattern);
            patternSegment.addElement(staticWord);
 
        }
 
        String prePattern = pattern.substring(previousIndex, pattern.length());
        String staticWord = "";
        patternSegment.addElement(prePattern);
        patternSegment.addElement(staticWord);
 
    }
 
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) {
        int size = patternSegment.size();
 
        for (int i = 0; i < size; i = i + 2) {
            String sp = (String) patternSegment.elementAt(i);
            String word = (String) patternSegment.elementAt(i + 1);
            if (!sp.equals("")) {
                format(date, toAppendTo, sp);
            }
            toAppendTo.append(word);
        }
 
        return toAppendTo;
 
    }
 
    private StringBuffer format(Date date, StringBuffer toAppendTo, String segmentPattern) {
 
        int index = 0;
        int length = 0;
        String rocYearPattern = null;
        if ((index = segmentPattern.indexOf("eee")) != -1) {
            length = 3;
            rocYearPattern = "000";
        }
 
        if (index == -1 && (index = segmentPattern.indexOf("ee")) != -1) {
            length = 2;
            rocYearPattern = "00";
        }
 
        if (index == -1) {
            SimpleDateFormat format = new SimpleDateFormat(segmentPattern);
            toAppendTo.append(format.format(date));
            return toAppendTo;
        } else {
            String pt1 = segmentPattern.substring(0, 0 + index);
 
            String pt2 = segmentPattern.substring(0 + index + length, segmentPattern.length());
            ROCDateTimeFormat format1 = new ROCDateTimeFormat(pt1);
            ROCDateTimeFormat format2 = new ROCDateTimeFormat(pt2);
 
            toAppendTo.append(format1.format(date));
            toAppendTo.append(getROCYear(date, rocYearPattern));
            toAppendTo.append(format2.format(date));
 
            return toAppendTo;
 
        }
    }
 
    private String getROCYear(Date date, String rocYearPattern) {
 
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        int rocyear = cal.get(Calendar.YEAR) - 1911;
 
        DecimalFormat format = new DecimalFormat(rocYearPattern);
        return format.format(rocyear);
    }
 
    public Date parse(String text, ParsePosition pos) {
        String parsedText = text;
        String parsedPattern = "";
        int size = patternSegment.size();
        
        for (int i = 0; i < size; i = i + 2) {
            String sp = (String) patternSegment.elementAt(i);
            String word = (String) patternSegment.elementAt(i + 1);
            parsedPattern += sp ;
            if(parsedText.indexOf(word)==-1){
                throw new RuntimeException("Illegal Text:"+text);
            }
            parsedText = parsedText.replaceFirst(word,"");
        }
 
        boolean rocHit = false;
        
        int rocDifference = 1911;
        
        if(parsedPattern.indexOf("eee")!=-1){
            rocHit = true;
            parsedPattern = parsedPattern.replaceAll("eee","yyy");
        }
        if(parsedPattern.indexOf("ee")!=-1){
            rocHit = true;
            parsedPattern = parsedPattern.replaceAll("ee","yy");
            rocDifference = 11;
        }
        
        
        
        
        SimpleDateFormat format = new SimpleDateFormat(parsedPattern);
        Date parsedDate =  format.parse(parsedText,pos);
        
        if(rocHit){
            Calendar cal = GregorianCalendar.getInstance();
            cal.setTime(parsedDate);
            cal.add(Calendar.YEAR,rocDifference);     
            parsedDate = cal.getTime();
        }
        
        return parsedDate;
 
    }
 
    public static void main(String[] args) {
        try {
            ROCDateTimeFormat format = new ROCDateTimeFormat("'ROC Year : 'ee 年  MM 月 dd 日 ' 時間是在 ' hh:mm:ss ");
            //ROCDateTimeFormat format = new ROCDateTimeFormat("eeeMMdd");
            String formated = format.format(new Date());
            System.out.println(formated);
            Date date = format.parse(formated);
            System.out.println(date);
            
            
            formated = format.format(date);
            System.out.println(formated);
            date = format.parse(formated);
            System.out.println(date);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 


atticcat edited on 2005-11-17 10:02
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
45233 [精華] 日期格式轉換 darkside 22 2004-01-16 12:02
43903 Re:日期格式轉換 louisz 693 2004-01-16 12:16
42546 Re:日期格式轉換 darkside 278 2004-01-16 15:23
16630 Re:日期格式轉換 wille 2704 2014-03-27 17:49
1467 Re:日期格式轉換 metavige 269 2020-06-06 20:58
42220 Re:日期格式轉換 liminjun 72 2004-01-16 15:53
41822 Re:日期格式轉換 browser 185 2004-01-16 15:58
41885 Re:日期格式轉換 darkside 211 2004-01-16 18:19
41484 Re:日期格式轉換 liminjun 265 2004-01-16 18:30
41710 Re:日期格式轉換 darkside 270 2004-01-16 21:42
41555 Re:日期格式轉換 liminjun 204 2004-01-17 00:54
44325 Re:日期格式轉換 - 完成 darkside 2827 2004-01-17 11:57
41379 Re:日期格式轉換 swpoker 64 2004-01-16 17:24
41567 Re:日期格式轉換 peizei 36 2004-01-29 11:51
41402 Re:日期格式轉換 tigerobj 347 2004-01-30 18:17
41615 Re:日期格式轉換 darkside 423 2004-01-30 23:42
40839 Re:日期格式轉換 ACheng 1683 2004-07-20 13:32
38547 民國年的DateFormat atticcat 5708 2005-11-16 18:27
37750 Re:日期格式轉換 enjoyit 134 2005-03-22 09:12
32436 Re:日期格式轉換 musicboy 878 2007-07-13 01:02
29122 Re:日期格式轉換 july081978 208 2007-11-05 14:49
29467 Re:日期格式轉換 taky 57 2007-11-06 10:31
» JWorld@TW »  Java SE 討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8