Formater.java
12.7 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package com.sitech.ibnms.util;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
public class Formater {
public final static String INVALIDDATE = "1950-01-01";
private final static String SQLTYPE = "SYBASE";
public final static String getNowTime() {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter1.format(new Date());
}
public final static String getToday() {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return formatter1.format(new Date());
}
public final static String getDateAsFile(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("MMddHHmmss");
return formatter1.format(dDate);
}
public final static String getSybaseFormatDate(String cDate) {
return cDate;
}
public final static String getSybaseFormatDate(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return formatter1.format(dDate);
}
public final static String getSybaseFormatDateTime(Date dDate) {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter1.format(dDate);
}
public final static String dateToString(Date dDate) throws ParseException {
if (null == dDate) {
return "";
}
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
if (formatter1.format(dDate).equals(INVALIDDATE)) {
return "";
}
return formatter1.format(dDate);
}
public final static String datetimeToString(Date date)
throws ParseException {
if (null == date) {
return "";
}
if (dateToString(date).equals("")) {
return "";
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(date);
}
public final static String datetimeToString(Date date, String formate) {
try{
if (null == date) {
return "";
}
SimpleDateFormat formatter = new SimpleDateFormat(formate);
return formatter.format(date);
}catch (Exception e) {
return "";
}
}
public static String counter64_sub(String newCounter, String oldCounter) {
String result = "0";
String counterType = "64";
if(newCounter==null||newCounter.equals("")||newCounter.equals("--"))
return "";
if(oldCounter==null||oldCounter.equals("")||oldCounter.equals("--"))
return "";
result = counter_sub(newCounter, oldCounter, counterType);
return result;
}
/**
*
* @param newCounter
* @param oldCounter
* @param counterType
* 32:32λ;64:64λ
* @return
*/
public static String counter_sub(String newCounter, String oldCounter,
String counterType) {
// 2^32 = 4294967295
// 2^64 = 18446744073709551615
String result = "0";
try {
BigInteger newbi = new BigInteger(newCounter);
BigInteger oldbi = new BigInteger(oldCounter);
BigInteger bi3 = new BigInteger("-1");
if (newbi.compareTo(oldbi) >= 0) {
// newbi >= oldbi
result = newbi.add(oldbi.divide(bi3)).toString();
} else {
BigInteger bi4 = null;
if (counterType.equals("32")) {
bi4 = new BigInteger("4294967295");
} else if (counterType.equals("64")) {
bi4 = new BigInteger("18446744073709551615");
}
result = bi4.add(oldbi.divide(bi3)).add(newbi).toString();
}
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return result;
}
public static String double2String(double d) {
int max_fraction_digits = 2;
int min_fraction_digits = 2;
return double2String(d, max_fraction_digits, min_fraction_digits);
}
public final static String fromDBTimeStamp(Timestamp ts, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(fromDBTimeStamp(ts));
}
public final static int string2int(String chs) {
int result=0;
try {
if(chs==null||chs.equals(""))
return result;
else
{
result= (new Integer(chs)).intValue();
}
} catch (NumberFormatException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return result;
}
public final static Date fromDBTimeStamp(Timestamp ts) {
Date result = new Date(ts.getTime());
return result;
}
/**
*
* @param newCounter
* @param oldCounter
* @return
*/
public static String counter32_sub(String newCounter, String oldCounter) {
String result = "0";
String counterType = "32";
if(newCounter==null||newCounter.equals("")||newCounter.equals("--"))
return "";
if(oldCounter==null||oldCounter.equals("")||oldCounter.equals("--"))
return "";
result = counter_sub(newCounter, oldCounter, counterType);
return result;
}
/**
* @return
*/
public static String double2String(double d, int max_fraction_digits,
int min_fraction_digits) {
String result = "";
try {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(max_fraction_digits);
nf.setMinimumFractionDigits(min_fraction_digits);
result = nf.format(d);
if(result.indexOf(",")!=-1)
result= result.replaceAll(",","");
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return result;
}
/* modified by wangxj on 2002/08/02 */
public final static String dateOnWeb(Date dDate) throws ParseException {
if (null == dDate) {
return "";
}
String cDate = dateToString(dDate);
if (cDate.equals("")) {
return "";
}
SimpleDateFormat formatter1 = new SimpleDateFormat("yy-MM-dd HH:mm");
return formatter1.format(dDate);
}
/* modified by xielj on 2002/08/11 */
public final static String timeOnWeb(Date dDate) throws ParseException {
if (null == dDate) {
return "";
}
String cDate = datetimeToString(dDate);
if (cDate.equals("")) {
return "";
}
SimpleDateFormat formatter1 = new SimpleDateFormat("HH:mm");
return formatter1.format(dDate);
}
public final static String timeOnWeb(String cDate) throws ParseException {
if (cDate.equals("")) {
return "";
}
SimpleDateFormat formatter1 = new SimpleDateFormat("HH:mm");
java.util.Date dDate = stringToDateTime(cDate);
return formatter1.format(dDate);
}
public final static Date stringToDate(String cDate, String cFormat) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat(cFormat);
return formatter1.parse(cDate);
}
public final static Date stringToDate(String cDate) throws ParseException {
try {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return formatter1.parse(cDate);
} catch (ParseException e) {
try {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter1.parse(cDate);
} catch (ParseException ee) {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
return formatter1.parse(cDate);
}
}
}
public final static Date stringToDateTime(String cDate) throws ParseException {
try {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter1.parse(cDate);
} catch (ParseException e) {
try {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
return formatter1.parse(cDate);
} catch (ParseException ee) {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return formatter1.parse(cDate);
}
}
}
public final static String toHTMLString(String chs) {
if (chs.equals("")) {
return chs;
} else {
StringTokenizer st = new StringTokenizer(chs, "\n");
String rt = new String("");
String cHTMLEnter = "<br>";
while (st.hasMoreTokens()) {
rt = rt + st.nextToken() + cHTMLEnter;
}
int i = 0;
while (true) {
if (rt.charAt(i) == 32) {
i++;
} else
break;
}
if (i > 0) {
String cSpace = "";
for (int j = 0; j < i; j++) {
cSpace = cSpace + " ";
}
rt = cSpace + rt.substring(i);
}
return rt;
}
}
public final static String ltrimtoHTMLString(String chs) {
StringTokenizer st = new StringTokenizer(chs, "\n");
String rt = new String("");
String cHTMLEnter = "<br>";
while (st.hasMoreTokens()) {
rt = rt + st.nextToken() + cHTMLEnter;
}
return rt;
}
/*
* public final static String toHTMLURL(String chs){ return
* java.net.URLEncoder.encode(chs); }
*/
/**
*/
public static String toHTMLBracket(String s) {
StringBuffer str = new StringBuffer();
int len = (s != null) ? s.length() : 0;
for (int i = 0; i < len; i++) {
char ch = s.charAt(i);
switch (ch) {
case '<': {
str.append("<");
break;
}
case '>': {
str.append(">");
break;
}
default: {
str.append(ch);
}
}
}
return str.toString();
}
public final static String fromSybaseString(String chs) throws java.io.UnsupportedEncodingException {
if (null == chs)
return "";
// String temp = new String(chs.getBytes("ISO8859_1"),"GBK");
return chs;
}
public final static String fromDBString(String chs) throws java.io.UnsupportedEncodingException {
if (null == chs)
return "";
String temp = chs;// new String(chs.getBytes("ISO8859_1"),"GBK");
return temp;
}
public final static String toSybaseString(String chs) throws java.io.UnsupportedEncodingException {
if (null == chs)
return "";
// String temp = new String(chs.getBytes("GBK"),"ISO8859_1");
return chs;
}
public final static String toDBString(String chs) throws java.io.UnsupportedEncodingException {
if (null == chs)
return "";
String temp = chs;// new String(chs.getBytes("GBK"),"ISO8859_1");
return temp;
}
// public final static String toOSString(String chs)
// throws java.io.UnsupportedEncodingException {
// if (chs == null)
// return "";
// String temp = new String(chs.getBytes("GBK"), OSCHARSET);
// return temp;
// }
public final static String toOracleDate(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return " to_date('" + formatter1.format(dDate) + "','yyyy-MM-dd') ";
}
public final static String toOracleDateTime(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return " to_date('" + formatter1.format(dDate) + "','yyyy-MM-dd HH24:mi:ss') ";
}
private final static String toSybaseDate(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
return " '" + formatter1.format(dDate) + "' ";
}
private final static String toSybaseDateTime(Date dDate) throws ParseException {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return " '" + formatter1.format(dDate) + "' ";
}
public final static String toDBDate(Date dDate) throws ParseException {
if (null == dDate) {
dDate = stringToDate(INVALIDDATE);
}
String cSQLDate = null;
if (SQLTYPE.equals("ORACLE"))
cSQLDate = toOracleDate(dDate);
if (SQLTYPE.equals("SYBASE"))
cSQLDate = toSybaseDate(dDate);
return cSQLDate;
}
public final static String toDBDate(String cDate) throws ParseException {
if (null == cDate) {
cDate = INVALIDDATE;
}
return toDBDate(stringToDate(cDate));
}
public final static String toDBDateTime(Date dDate) throws ParseException {
if (null == dDate) {
dDate = stringToDate(INVALIDDATE);
}
String cSQLDate = null;
if (SQLTYPE.equals("ORACLE"))
cSQLDate = toOracleDateTime(dDate);
if (SQLTYPE.equals("SYBASE"))
cSQLDate = toSybaseDateTime(dDate);
return cSQLDate;
}
public final static String toDBDateTime(String cDate) throws ParseException {
if (null == cDate) {
cDate = INVALIDDATE;
}
return toDBDateTime(stringToDateTime(cDate));
}
public final static String getFormattedCLL_TIME(String CLL_TIME) {
String cllTime = "";
SimpleDateFormat formatter1 = null;
SimpleDateFormat formatter2 = new SimpleDateFormat("MM-dd HH:mm");
java.util.Date dCLL_TIME = null;
try {
formatter1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
dCLL_TIME = formatter1.parse(CLL_TIME);
} catch (ParseException e) {
try {
formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dCLL_TIME = formatter1.parse(CLL_TIME);
} catch (ParseException e1) {
// Loger.errLog(e);
}
}
if (dCLL_TIME != null)
cllTime = formatter2.format(dCLL_TIME);
return cllTime;
}
}