DateUtil.java
71.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
package th.co.ais.ssbsrfc.utils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Random;
import java.util.TimeZone;
public class DateUtil {
protected final static long ONEDAY = 10 * 10 * 10 * 24 * 60 * 60;
static long w_oneDay = (long) 1000.0 * 60 * 60 * 24;
static String vLocale_defined = "TH";
public static final String yyyyMMdd = "yyyy-MM-dd";
public static final String ddMMyyyy = "dd/MM/yyyy";
public static final String ddMMMMyyyy = "dd MMMM yyyy";
public static final String ddMMyyyyHHmiss = "dd MMMM yyyy HH:mm:ss";
public static final String yyyyMMddHHmiss = "yyyy-MM-dd HH:mm:ss";
public static final String ddMMyyyyHHmi = "dd MMMM yyyy HH:mm";
public static Calendar getCurrentDateTime() {
Calendar cal = new GregorianCalendar();
return cal;
}
public static String getDateExpiryTime(String startTime, int numDay) {
String newCal = null;
java.util.Date date = null;
Calendar c;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd:HHmmss");
try {
date = (Date)formatter.parse(startTime);
c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, numDay);
newCal = formatter.format(c.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newCal;
}
public static String getDateExpiryTimeZone(String startTime, int numDay) {
String newCal = null;
java.util.Date date = null;
Calendar c;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssZ");
try {
date = (Date)formatter.parse(startTime);
c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, numDay);
newCal = formatter.format(c.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newCal;
}
public static String getExpiryTime(String startTime, int sec) {
String datetime = null;
java.util.Date date = null;
Calendar c;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd:HHmmss");
try {
date = (Date)formatter.parse(startTime);
c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.SECOND, sec);
//System.out.println("calendar"+c.getTime());
datetime = formatter.format(c.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return datetime;
}
public static String getExpiryTimeZone(String startTime, int sec) {
String datetime = null;
java.util.Date date = null;
Calendar c;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssZ");
try {
date = (Date)formatter.parse(startTime);
c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.SECOND, sec);
//System.out.println("calendar"+c.getTime());
datetime = formatter.format(c.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return datetime;
}
/*public static String getDateExpiryTime(int numDay, int numMonth, int numYear) {
String oldCal = null;
String newCal = null;
oldCal = getCurrentDateTime().get(Calendar.YEAR) + "/"
+ (getCurrentDateTime().get(Calendar.MONTH)+1) + "/"
+ getCurrentDateTime().get(Calendar.DAY_OF_MONTH) + " "
+ getCurrentDateTime().get(Calendar.HOUR_OF_DAY) + ":"
+ getCurrentDateTime().get(Calendar.MINUTE) + ":"
+ getCurrentDateTime().get(Calendar.SECOND);
SimpleDateFormat dfold = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar c = Calendar.getInstance();
try {
Date date = dfold.parse(oldCal);
c.setTime(date);
c.add(Calendar.DATE, numDay);
c.add(Calendar.MONTH, numMonth);
c.add(Calendar.YEAR, numYear);
SimpleDateFormat dfnew = new SimpleDateFormat("yyyyMMdd:HHmmss");
newCal = dfnew.format(c.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newCal;
}*/
public static String getDateTimeNow() {
String oldCal = null;
String newCal = null;
oldCal = getCurrentDateTime().get(Calendar.YEAR) + "/"
+ (getCurrentDateTime().get(Calendar.MONTH)+1) + "/"
+ getCurrentDateTime().get(Calendar.DAY_OF_MONTH) + " "
+ getCurrentDateTime().get(Calendar.HOUR_OF_DAY) + ":"
+ getCurrentDateTime().get(Calendar.MINUTE) + ":"
+ getCurrentDateTime().get(Calendar.SECOND);
SimpleDateFormat dfold = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Date date = dfold.parse(oldCal);
SimpleDateFormat dfnew = new SimpleDateFormat("yyyyMMdd:HHmmss");
newCal = dfnew.format(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newCal;
}
public static String getDateTimeZoneNow() {
String oldCal = null;
String newCal = null;
oldCal = getCurrentDateTime().get(Calendar.YEAR) + "/"
+ (getCurrentDateTime().get(Calendar.MONTH)+1) + "/"
+ getCurrentDateTime().get(Calendar.DAY_OF_MONTH) + " "
+ getCurrentDateTime().get(Calendar.HOUR_OF_DAY) + ":"
+ getCurrentDateTime().get(Calendar.MINUTE) + ":"
+ getCurrentDateTime().get(Calendar.SECOND);
SimpleDateFormat dfold = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Date date = dfold.parse(oldCal);
SimpleDateFormat dfnew = new SimpleDateFormat("yyyyMMddHHmmssZ");
newCal = dfnew.format(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newCal;
}
public static String getDateTimeNowForWriteFile() {
String oldCal = null;
String newCal = null;
oldCal = getCurrentDateTime().get(Calendar.YEAR) + "/"
+ (getCurrentDateTime().get(Calendar.MONTH)+1) + "/"
+ getCurrentDateTime().get(Calendar.DAY_OF_MONTH) + " "
+ getCurrentDateTime().get(Calendar.HOUR_OF_DAY) + ":"
+ getCurrentDateTime().get(Calendar.MINUTE) + ":"
+ getCurrentDateTime().get(Calendar.SECOND);
SimpleDateFormat dfold = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Date date = dfold.parse(oldCal);
SimpleDateFormat dfnew = new SimpleDateFormat("yyyyMMdd|HHmm");
newCal = dfnew.format(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newCal;
}
public static String getDateTimeNowYYYYMMdd() {
String oldCal = null;
String newCal = null;
oldCal = getCurrentDateTime().get(Calendar.YEAR) + "/"
+ (getCurrentDateTime().get(Calendar.MONTH)+1) + "/"
+ getCurrentDateTime().get(Calendar.DAY_OF_MONTH) + " "
+ getCurrentDateTime().get(Calendar.HOUR_OF_DAY) + ":"
+ getCurrentDateTime().get(Calendar.MINUTE) + ":"
+ getCurrentDateTime().get(Calendar.SECOND);
SimpleDateFormat dfold = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Date date = dfold.parse(oldCal);
SimpleDateFormat dfnew = new SimpleDateFormat("yyyyMMdd");
newCal = dfnew.format(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newCal;
}
public static String getDateTimeNow_ddMMyyyy_HHmmss() {
String newCal = null;
newCal = getCurrentDateTime().get(Calendar.DAY_OF_MONTH) + "/"
+ getMonthStr(getCurrentDateTime().get(Calendar.MONTH)) + "/"
+ getCurrentDateTime().get(Calendar.YEAR) + " "
+ getCurrentDateTime().get(Calendar.HOUR_OF_DAY) + ":"
+ getCurrentDateTime().get(Calendar.MINUTE) + ":"
+ getCurrentDateTime().get(Calendar.SECOND);
return newCal;
}
public static int getCurrentDay() {
return getCurrentDateTime().get(Calendar.DAY_OF_MONTH);
}
public static int getCurrentDayOfWeek() {
return getCurrentDateTime().get(Calendar.DAY_OF_WEEK);
}
public static int getCurrentMonth() {
return getCurrentDateTime().get(Calendar.MONTH);
}
public static int getCurrentYear() {
return getCurrentDateTime().get(Calendar.YEAR);
}
public static int getCurrentHour() {
return getCurrentDateTime().get(Calendar.HOUR_OF_DAY);
}
public static int getCurrentMinute() {
return getCurrentDateTime().get(Calendar.MINUTE);
}
public static int getCurrentSecond() {
return getCurrentDateTime().get(Calendar.SECOND);
}
public synchronized static long getCurrentDateTimeinLong() {
Calendar cal = new GregorianCalendar();
return cal.getTime().getTime();
}
/* getCurrentDateTime by pattern format */
public static String getStringSystemDateTime(String pattern) {
Calendar currentCalendar = DateUtil.getCurrentDateTime();
String systemDateTime = DateUtil.convertDateTime2String(
currentCalendar, pattern);
return systemDateTime;
}
/* convert Calendar by pattern format */
public static String convertDateTime2String(Calendar calendar,
String pattern) {
String data = null;
if (calendar != null) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
data = sdf.format(calendar.getTime());
}
return data;
}
public static String convertDateTime2String(Calendar calendar,
String pattern, Locale local) {
String data = null;
if (calendar != null) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern, local);
data = sdf.format(calendar.getTime());
}
return data;
}
public static String convertDateTime2String(Date calendar, String pattern,
Locale local) {
String data = null;
if (calendar != null) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern, local);
data = sdf.format(calendar.getTime());
}
return data;
}
public static String convertDateTime2String(Timestamp calendar, String pattern,
Locale local) {
String data = null;
if (calendar != null) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern, local);
data = sdf.format(calendar.getTime());
}
return data;
}
public static Calendar getCalendar(long time) {
Calendar cal = new GregorianCalendar();
cal.setTime(new Date(time));
return cal;
}
public static Calendar getCalendar(int day, int month, int year) {
return getCalendar(day, month, year, 0, 0, 0);
}
public static Calendar getCalendar(int day, int month, int year, int hour,
int minute) {
return getCalendar(day, month, year, hour, minute, 0);
}
public static Calendar getCalendar(int day, int month, int year, int hour,
int minute, int second) {
Calendar cal = null;
if (day >= 0 && month >= 0 && year >= 0 && hour >= 0 && minute >= 0) {
cal = new GregorianCalendar(Locale.US);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
cal.set(Calendar.MILLISECOND, 0);
}
// cal.add(Calendar.HOUR_OF_DAY, plus);
return cal;
}
/**
* Convert from java.lang.Object to java.util.Date Creation date:
* (26/05/2004 11:00:00)
*
* @return java.util.Date
* @param theDateObj
* java.lang.Object
*/
public static java.util.Date getDate(Object theDateObj) {
java.sql.Date w_TempDate;
if (theDateObj == null) {
w_TempDate = null;
} else {
java.util.Date theDateDate = (java.util.Date) theDateObj;
w_TempDate = new java.sql.Date(theDateDate.getTime());
}
return w_TempDate;
}
/**
* Convert from java.lang.Object to java.util.Date. Creation date:
* (26/05/2004 11:00:00)
*
* @return java.util.Date
* @param theDateObj,theTimeObj
* java.lang.Object
*/
public static java.util.Date getDate(Object theDateObj, Object theTimeObj) {
java.util.Date w_TempDate = null;
java.sql.Date w_ArgDate = null;
if (theDateObj != null) {
w_ArgDate = (java.sql.Date) theDateObj;
java.util.GregorianCalendar w_DateGregorian = new java.util.GregorianCalendar();
w_DateGregorian.setTime(w_ArgDate);
if (theTimeObj != null) {
java.util.GregorianCalendar w_TimeGregorian = new java.util.GregorianCalendar();
w_TimeGregorian.setTime((DateUtil.getDate(theTimeObj)));
w_DateGregorian.set(GregorianCalendar.HOUR_OF_DAY,
w_TimeGregorian.get(GregorianCalendar.HOUR_OF_DAY));
w_DateGregorian.set(GregorianCalendar.MINUTE, w_TimeGregorian
.get(GregorianCalendar.MINUTE));
w_DateGregorian.set(GregorianCalendar.SECOND, w_TimeGregorian
.get(GregorianCalendar.SECOND));
w_TimeGregorian = null;
}
w_TempDate = w_DateGregorian.getTime();
w_DateGregorian = null;
}
w_ArgDate = null;
return w_TempDate;
}
/** *************************************************************************** */
/**
* Convert from java.lang.String to java.util.Date. Creation date:
* (26/05/2004 11:30:03)
*
* @return java.lang.String
* @param theDateStr
* java.util.Date
*/
/*public static java.util.Date getDate(String theDateStr) throws Exception {
Date w_TempDate = null;
Vector w_VecterDate = null;
String vLocale = null;
if (theDateStr != null && !theDateStr.trim().equals("")) {
w_VecterDate = StringUtil.getStringByDelimeter(theDateStr, "/");
java.util.GregorianCalendar w_TempGregorian = null;
vLocale = vLocale_defined;
if ("TH".equals(vLocale)) {
w_TempGregorian = new java.util.GregorianCalendar(
NumberUtil.getInt((String) w_VecterDate.elementAt(2)) - 543,
NumberUtil.getInt((String) w_VecterDate.elementAt(1)) - 1,
NumberUtil.getInt((String) w_VecterDate.elementAt(0)));
} else {
w_TempGregorian = new java.util.GregorianCalendar(NumberUtil
.getInt((String) w_VecterDate.elementAt(2)), NumberUtil
.getInt((String) w_VecterDate.elementAt(1)) - 1,
NumberUtil.getInt((String) w_VecterDate.elementAt(0)));
}
w_TempDate = w_TempGregorian.getTime();
w_TempGregorian = null;
}
w_VecterDate = null;
return w_TempDate;
}
public static java.util.Date getDateDelimerterByDash(String theDateStr)
throws Exception {
Date w_TempDate = null;
// Vector w_VecterDate = null;
String vLocale = null;
if (theDateStr != null && !theDateStr.trim().equals("")) {
// Vector w_Vecter = StringUtil.getStringByDelimeter(theDateStr,"
// ");
String[] tempSplit = theDateStr.split(" ");
Vector w_VecterDate = null;
w_VecterDate = StringUtil.getStringByDelimeter(tempSplit[0], "-");
Vector w_VecterTime = null;
try {
w_VecterTime = StringUtil.getStringByDelimeter(theDateStr
.split(" ")[1], ":");
} catch (Exception ex) {
}
java.util.GregorianCalendar w_TempGregorian = null;
vLocale = vLocale_defined;
// if("TH".equals(vLocale)){
// w_TempGregorian = new
// java.util.GregorianCalendar(NumberUtil.getInt((String)w_VecterDate.elementAt(2))-543,NumberUtil.getInt((String)w_VecterDate.elementAt(1))
// - 1,NumberUtil.getInt((String)w_VecterDate.elementAt(0)));
// }else{
w_TempGregorian = new java.util.GregorianCalendar(NumberUtil
.getInt((String) w_VecterDate.elementAt(0)), NumberUtil
.getInt((String) w_VecterDate.elementAt(1)) - 1, NumberUtil
.getInt((String) w_VecterDate.elementAt(2)));
if (w_VecterTime != null) {
w_TempGregorian.set(NumberUtil.getInt((String) w_VecterDate
.elementAt(0)), NumberUtil.getInt((String) w_VecterDate
.elementAt(1)) - 1, NumberUtil
.getInt((String) w_VecterDate.elementAt(2)), NumberUtil
.getInt((String) w_VecterTime.elementAt(0)), NumberUtil
.getInt((String) w_VecterTime.elementAt(1)), NumberUtil
.getInt((String) w_VecterTime.elementAt(2)));
}
// }
w_TempDate = w_TempGregorian.getTime();
w_TempGregorian = null;
w_VecterDate = null;
w_VecterTime = null;
}
return w_TempDate;
}*/
/**
* Convert from java.lang.String to java.util.Date. Creation date:
* (11/05/2004 08:30:03)
*
* @return java.lang.String
* @param theDateStr,theTimeStr
* java.util.Date
*/
/*public static java.util.Date getDate(String theDateStr, String theTimeStr)
throws Exception {
Date w_TempDate = null;
Vector w_DateVector = null;
Vector w_TimeVector = null;
int w_DateInt = 0, w_YearInt = 0, w_MonthInt = 0, w_MinutehInt = 0, w_HourInt = 0;
if (theDateStr != null) {
w_DateVector = StringUtil.getStringByDelimeter(theDateStr, "/");
w_YearInt = NumberUtil.getInt((String) w_DateVector.elementAt(2));
w_MonthInt = NumberUtil.getInt((String) w_DateVector.elementAt(1)) - 1;
w_DateInt = NumberUtil.getInt((String) w_DateVector.elementAt(0));
w_DateVector = null;
if (theTimeStr != null) {
w_TimeVector = StringUtil.getStringByDelimeter(theTimeStr, ":");
w_HourInt = NumberUtil.getInt((String) w_TimeVector
.elementAt(0));
w_MinutehInt = NumberUtil.getInt((String) w_TimeVector
.elementAt(1));
w_TimeVector = null;
}
java.util.GregorianCalendar w_TempGregorian = new java.util.GregorianCalendar(
w_YearInt, w_MonthInt, w_DateInt, w_HourInt, w_MinutehInt);
w_TempDate = w_TempGregorian.getTime();
w_TempGregorian = null;
}
w_DateVector = null;
w_TimeVector = null;
return w_TempDate;
}*/
/**
* Convert from java.sql.Date and java.sql.Time to java.util.Date. Creation
* date: (26/05/2004 11:40:03)
*
* @return java.util.Date
* @param theDateDate
* java.sql.Date ,theTimeDate java.sql.Time
*/
public static java.util.Date getDate(java.sql.Date theDateDate,
java.sql.Time theTimeDate) {
java.util.Date w_TempDate = null;
if (theDateDate == null) {
w_TempDate = null;
} else {
java.util.GregorianCalendar w_DateGregorian = new java.util.GregorianCalendar();
w_DateGregorian.setTime(theDateDate);
if (theTimeDate != null) {
java.util.GregorianCalendar w_TimeGregorian = new java.util.GregorianCalendar();
w_TimeGregorian.setTime((DateUtil.getDate(theTimeDate)));
w_DateGregorian.set(GregorianCalendar.HOUR_OF_DAY,
w_TimeGregorian.get(GregorianCalendar.HOUR_OF_DAY));
w_DateGregorian.set(GregorianCalendar.MINUTE, w_TimeGregorian
.get(GregorianCalendar.MINUTE));
w_DateGregorian.set(GregorianCalendar.SECOND, w_TimeGregorian
.get(GregorianCalendar.SECOND));
}
w_TempDate = w_DateGregorian.getTime();
w_DateGregorian = null;
}
return w_TempDate;
}
public static java.util.Date getTime(Object theDateObj) {
java.sql.Time w_TempDate;
if (theDateObj == null) {
w_TempDate = null;
} else {
java.util.Date theDateDate = (java.util.Date) theDateObj;
w_TempDate = new java.sql.Time(theDateDate.getTime());
}
return w_TempDate;
}
@SuppressWarnings("unchecked")
public static Collection getWeekend(Calendar beginCal, Calendar endCal) {
Calendar begin = getCalendar(beginCal.get(Calendar.DAY_OF_MONTH),
beginCal.get(Calendar.MONTH), beginCal.get(Calendar.YEAR), 0,
0, 0);
Calendar end = getCalendar(endCal.get(Calendar.DAY_OF_MONTH), endCal
.get(Calendar.MONTH), endCal.get(Calendar.YEAR), 0, 0, 0);
int day = begin.get(Calendar.DAY_OF_WEEK);
Collection weekendCol = new ArrayList();
int nextSat = 6;
if (day == Calendar.SUNDAY) {
weekendCol.add(getCalendar(begin.get(Calendar.DAY_OF_MONTH), begin
.get(Calendar.MONTH), begin.get(Calendar.YEAR)));
} else if (day == Calendar.SATURDAY) {
weekendCol.add(getCalendar(begin.get(Calendar.DAY_OF_MONTH), begin
.get(Calendar.MONTH), begin.get(Calendar.YEAR)));
begin.add(Calendar.DAY_OF_MONTH, 1);
if (begin.before(end) || begin.equals(end)) {
weekendCol.add(getCalendar(begin.get(Calendar.DAY_OF_MONTH),
begin.get(Calendar.MONTH), begin.get(Calendar.YEAR)));
}
} else {
nextSat = 7 - day;
}
begin.add(Calendar.DAY_OF_MONTH, nextSat);
for (; begin.before(end) || begin.equals(end); begin.add(
Calendar.DAY_OF_MONTH, 6)) {
weekendCol.add(getCalendar(begin.get(Calendar.DAY_OF_MONTH), begin
.get(Calendar.MONTH), begin.get(Calendar.YEAR)));
begin.add(Calendar.DAY_OF_MONTH, 1);
if (begin.before(end) || begin.equals(end)) {
weekendCol.add(getCalendar(begin.get(Calendar.DAY_OF_MONTH),
begin.get(Calendar.MONTH), begin.get(Calendar.YEAR)));
}
}
return weekendCol;
}
/**
* Insert the method's description here. Creation date: (11/3/2005 9:59:23)
*
* @return int
* @param d
* java.util.Date
*/
public static Date getLastDateOfMonth(Date firstDateOfMonth) {
Date newDate = null;
Date newDate2 = null;
try {
newDate = addMonth(firstDateOfMonth, true, 1);
newDate2 = addDate(newDate, false, 1);
} catch (Exception e) {
e.printStackTrace();
}
return newDate2;
}
/**
* Convert from java.util.Date to java.sql.Date. Creation date: (26/05/2004
* 13:00:03)
*
* @return java.sql.Date
* @param theDateDate
* java.util.Date
*/
public static java.sql.Date getDBDate(java.util.Date theDateDate) {
java.sql.Date w_TempDate;
if (theDateDate == null)
w_TempDate = null;
else {
w_TempDate = new java.sql.Date(theDateDate.getTime());
}
return w_TempDate;
}
/**
* Convert from String to java.sql.Date. Creation date: (26/05/2004
* 13:00:03)
*
* @return java.sql.Date
* @param theDate
* String
*/
public static java.sql.Date getDBDate(String theDate) throws Exception {
return getDBDate(getDate(theDate));
}
/**
* Convert from Timestamp to java.sql.Date. Creation date: (26/05/2004
* 13:00:03)
*
* @return java.sql.Date
* @param theTimestamp
* Timestamp
*/
public static java.sql.Date getDBDate(Timestamp theTimestamp)
throws Exception {
return getDBDate(new java.sql.Date(theTimestamp.getTime()));
}
/*
* Format is DD/MM/YYYY:HH:MI 10/9/2005:08:00 // public static Calendar
* getCalendar(String calStr){
*
* StringTokenizer token = new StringTokenizer(calStr,"/"); String dd =
* token.nextToken(); String mm = token.nextToken(); String yy =
* token.nextToken(":").replace('/',' ').trim(); String hh =
* token.nextToken(":"); String mi = token.nextToken(":");
*
* return getCalendar(Integer.parseInt(dd),Integer.parseInt(mm),
* Integer.parseInt(yy),Integer.parseInt(hh),Integer.parseInt(mi)); }
*/
/*public static java.sql.Time getDBTime(String theTimeStr) throws Exception {
java.sql.Time w_time = null;
java.util.Vector w_TimeVector = null;
int w_HourInt = 0;
int w_MinuteInt = 0;
if (theTimeStr != null) {
w_TimeVector = StringUtil.getStringByDelimeter(theTimeStr, ":");
w_HourInt = NumberUtil.getInt((String) w_TimeVector.elementAt(0));
w_MinuteInt = NumberUtil.getInt((String) w_TimeVector.elementAt(1));
w_TimeVector = null;
}
java.util.GregorianCalendar w_TempGregorian = new java.util.GregorianCalendar(
0, 0, 0, w_HourInt, w_MinuteInt);
w_time = new Time(w_TempGregorian.getTime().getTime());
return w_time;
}*/
public static Timestamp getDBTimestamp(java.util.Date theDate)
throws Exception {
Timestamp w_timestamp = null;
if (theDate != null) {
w_timestamp = new Timestamp(theDate.getTime());
} else {
w_timestamp = new Timestamp(new java.util.Date().getTime());
}
return w_timestamp;
}
/*public static Timestamp getDBTimestamp(Object theDate) throws Exception {
Timestamp w_timestamp = null;
if (theDate != null && !theDate.equals("")) {
w_timestamp = new Timestamp(
getDBDate(StringUtil.getString(theDate)).getTime());
} else {
w_timestamp = null;
}
return w_timestamp;
}*/
/*
* public static HashMap getYearMonthDay(java.sql.Date theStartDate ,
* java.sql.Date theEndDate){ int w_start_day = theStartDate.getDate(); int
* w_start_month = theStartDate.getMonth()+1; int w_start_year =
* theStartDate.getYear()+1900; int w_end_day = theEndDate.getDate(); int
* w_end_month = theEndDate.getMonth()+1; int w_end_year =
* theEndDate.getYear()+1900; HashMap w_result = null; int w_year = 0 ; int
* w_month =0; int w_day = 0; int w_diff_year = 0; int w_diff_month = 0;
*
* w_diff_year = w_end_year - w_start_year; w_diff_month = w_end_month -
* w_start_month; switch(w_diff_year){
*
* case 0 : w_year = w_diff_year; w_month = w_diff_month -1; break; case 1 :
* w_year = w_diff_year - 1; w_month = w_end_month + (12 - w_start_month) -
* 1; break; default : w_year = w_diff_year - 1; w_month = w_end_month + (12 -
* w_start_month) - 1; break;
*
* }//switch(YEAR)
*
* switch(w_start_month){ case 1 : case 3 : case 5 : case 7 : case 8 : case
* 10 : case 12 : w_day = w_end_day + (31 - w_start_day)+ 1; if (w_day >
* 31){ w_month = w_month + 1; w_day = w_day - 31; }; break; case 2 : if
* ((w_start_year % 4) == 0) { w_day = w_end_day + (29 - w_start_day)+1; if
* (w_day > 29){ w_month = w_month + 1; w_day = w_day - 29; } }else{ w_day =
* w_end_day + (28-w_start_day)+1; if(w_day > 28){ w_month = w_month +1;
* w_day = w_day - 28; } };//end case 2: break;
*
* case 4 : case 6 : case 9 : case 11 : w_day = w_end_day + (30 -
* w_start_day) +1; if(w_day > 30){ w_month = w_month +1; w_day = w_day -
* 31; }; break; }//switch(START MONTH) switch(w_end_month){ case 1 : case 3 :
* case 5 : case 7 : case 8 : case 10 : case 12 : if(w_day == 31){ w_month =
* w_month + 1; w_day = 0; } break; case 2 : if ((w_start_year % 4) == 0) {
* if(w_day == 29){ w_month = w_month + 1; w_day = 0; } }else{ if((w_day ==
* 28) || (w_day == 29)){ w_month = w_month + 1; w_day = 0; } };//end case
* 2: break;
*
* case 4 : case 6 : case 9 : case 11 : if(w_day == 30){ w_month = w_month +
* 1; w_day = 0; } break; }//switch(STOP MONTH) if(w_month >= 24){ w_year =
* w_year + 2; w_month = w_month - 24; }else{ if(w_month >=12){ w_year =
* w_year + 1; w_month = w_month - 12; } }
*
* w_result = new HashMap();
* w_result.put("YEAR",NumberUtil.getInteger(w_year));
* w_result.put("MONTH",NumberUtil.getInteger(w_month));
* w_result.put("DAY",NumberUtil.getInteger(w_day)); return w_result;
* }//getYearMonthDay
*/
public static int getYear(Calendar cal) {
return cal.get(Calendar.YEAR);
}
/**
* Insert the method's description here. Creation date: (16/8/2001 15:42:23)
*
* @return int
* @param d
* java.util.Date
*/
public static int getYear(Date d) {
java.util.GregorianCalendar temp = new java.util.GregorianCalendar();
temp.setTime(d);
return temp.get(GregorianCalendar.YEAR);
}
public static int getMonth(Calendar cal) {
return cal.get(Calendar.MONTH);
}
/**
* Insert the method's description here. Creation date: (16/8/2001 15:42:23)
*
* @return int
* @param d
* java.util.Date
*/
/*public static int getMonth(Date d) throws Exception {
String w_date = StringUtil.getLocaleDate(d);
java.util.Date w_d = DateUtil.getDate(w_date);
java.util.GregorianCalendar temp = new java.util.GregorianCalendar();
temp.setTime(w_d);
return temp.get(GregorianCalendar.MONTH) + 1;
}*/
public static int getDay(Calendar cal) {
return cal.get(Calendar.DAY_OF_MONTH);
}
public static int getDayOfWeek(Calendar cal) {
return cal.get(Calendar.DAY_OF_WEEK);
}
public static int getDayOfYear(Timestamp theTimestamp) throws Exception {
int w_total_day = 0;
Date w_util_date = DateUtil.getDate(theTimestamp);
// GregorianCalendar w_gre_calendar = new GregorianCalendar();
w_total_day = getDayOfYear(w_util_date);
return w_total_day;
}
public static int getDayOfYear(String theDate) throws Exception {
int w_total_day = 0;
Date w_util_date = DateUtil.getDate(theDate);
w_total_day = getDayOfYear(w_util_date);
return w_total_day;
}
@SuppressWarnings("deprecation")
public static int getDayOfYear(Date theDate) throws Exception {
int w_total_day = 0;
GregorianCalendar w_gre_calendar = new GregorianCalendar();
if (w_gre_calendar.isLeapYear(theDate.getYear())) {
w_total_day = 366;
} else {
w_total_day = 365;
}
return w_total_day;
}
public static int getHour(Calendar cal) {
return cal.get(Calendar.HOUR_OF_DAY);
}
public static int getMinute(Calendar cal) {
return cal.get(Calendar.MINUTE);
}
public static int getSecond(Calendar cal) {
return cal.get(Calendar.SECOND);
}
public static void setDay(Calendar cal, int day) {
cal.set(Calendar.DAY_OF_MONTH, day);
}
public static void setMonth(Calendar cal, int month) {
cal.set(Calendar.MONTH, month);
}
public static void setYear(Calendar cal, int year) {
cal.set(Calendar.YEAR, year);
}
public static void setHour(Calendar cal, int hour) {
cal.set(Calendar.HOUR_OF_DAY, hour);
}
public static void setMinute(Calendar cal, int minute) {
cal.set(Calendar.MINUTE, minute);
}
public static void setSecond(Calendar cal, int second) {
cal.set(Calendar.SECOND, second);
}
public static void setMilli(Calendar cal, int milli) {
cal.set(Calendar.MILLISECOND, milli);
}
public static boolean equal(Calendar cal1, Calendar cal2) {
Calendar c1 = getCalendar(cal1.get(Calendar.DAY_OF_MONTH), cal1
.get(Calendar.MONTH), cal1.get(Calendar.YEAR), cal1
.get(Calendar.HOUR), cal1.get(Calendar.MINUTE), cal1
.get(Calendar.SECOND));
Calendar c2 = getCalendar(cal2.get(Calendar.DAY_OF_MONTH), cal2
.get(Calendar.MONTH), cal2.get(Calendar.YEAR), cal2
.get(Calendar.HOUR), cal2.get(Calendar.MINUTE), cal2
.get(Calendar.SECOND));
return c1.equals(c2);
}
public static java.util.Date addDate(java.util.Date theDate, boolean add,
int amount) throws Exception {
if (theDate == null)
throw new Exception("theDate is null");
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(theDate);
if (add)
calendar.add(GregorianCalendar.DATE, amount);
else
calendar.add(GregorianCalendar.DATE, (-1) * amount);
return calendar.getTime();
}
public static java.util.Date addSecond(java.util.Date theDate,boolean add, int amount) throws Exception {
if(theDate == null)
throw new Exception ("theDate is null");
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(theDate);
if(add)
calendar.add(GregorianCalendar.SECOND,amount);
else
calendar.add(GregorianCalendar.SECOND,(-1)*amount);
return calendar.getTime();
}
public static java.util.Date addMonth(java.util.Date theDate, boolean add,
int amount) throws Exception {
if (theDate == null)
throw new Exception("theDate is null");
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(theDate);
if (add)
calendar.add(GregorianCalendar.MONTH, amount);
else
calendar.add(GregorianCalendar.MONTH, (-1) * amount);
return calendar.getTime();
}
public static java.util.Date addYear(java.util.Date theDate, boolean add,
int amount) throws Exception {
if (theDate == null)
throw new Exception("theDate is null");
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(theDate);
if (add)
calendar.add(GregorianCalendar.YEAR, amount);
else
calendar.add(GregorianCalendar.YEAR, (-1) * amount);
return calendar.getTime();
}
public static void addDay(Calendar cal, int i) {
cal.add(Calendar.DAY_OF_MONTH, i);
}
public static void addHour(Calendar cal, int i) {
cal.add(Calendar.HOUR_OF_DAY, i);
}
public static void addMinute(Calendar cal, int i) {
cal.add(Calendar.MINUTE, i);
}
// public static long diffDate(Calendar d1,Calendar d2) {
// Calendar c1 = getCalendar(d1.get(Calendar.DAY_OF_MONTH),
// d1.get(Calendar.MONTH), d1.get(Calendar.YEAR),0,0,0);
// Calendar c2 = getCalendar(d2.get(Calendar.DAY_OF_MONTH),
// d2.get(Calendar.MONTH), d2.get(Calendar.YEAR),0,0,0);
// long diffInMilliSeconds=c1.getTime().getTime()-c2.getTime().getTime();
// return diffInMilliSeconds/(1000*60*60*24);
// }
public static int diffDate(Calendar d1, Calendar d2) {
Calendar c1 = getCalendar(d1.get(Calendar.DAY_OF_MONTH), d1
.get(Calendar.MONTH), d1.get(Calendar.YEAR), 0, 0, 0);
Calendar c2 = getCalendar(d2.get(Calendar.DAY_OF_MONTH), d2
.get(Calendar.MONTH), d2.get(Calendar.YEAR), 0, 0, 0);
long diffInMilliSeconds = c1.getTime().getTime()
- c2.getTime().getTime();
int diffDays = (int) (diffInMilliSeconds / (1000 * 60 * 60 * 24));
return diffDays;
}
public static int diffDate(Date dateStart, Date dateEnd) throws Exception {
Calendar calDateStart = new GregorianCalendar();
calDateStart.set(Calendar.MONTH, dateStart.getMonth());
calDateStart.set(Calendar.DATE, dateStart.getDate());
calDateStart.set(Calendar.YEAR, dateStart.getYear());
Calendar calDateEnd = new GregorianCalendar();
calDateEnd.set(Calendar.MONTH, dateEnd.getMonth());
calDateEnd.set(Calendar.DATE, dateEnd.getDate());
calDateEnd.set(Calendar.YEAR, dateEnd.getYear());
return diffDate(calDateStart, calDateEnd);
}
public static String diffDateStringThai(Date dateStart, Date dateEnd)
throws Exception {
String strDiffDateThai = "";
int daysDiff = diffDate(dateStart, dateEnd);
System.out.println("pass 764 - dateStart=" + dateStart.toString()
+ " dateEnd=" + dateEnd.toString());
System.out.println("pass 764 - daysDiff=" + daysDiff);
double dblYearsDiff = daysDiff / 365.25;
int intYearsDiff = (int) (dblYearsDiff);
double dblMonthDiff = (daysDiff % 365.25) / 30.4375;
int intMonthDiff = (int) dblMonthDiff;
double dblDayDiff = (daysDiff % 365.25) % 30.4375;
int intDayDiff = (int) dblDayDiff;
strDiffDateThai = intYearsDiff + " ปี " + intMonthDiff
+ " เดืà¸à¸™ " + intDayDiff + " วัน ";
return strDiffDateThai;
}
public static String diffDateStringEng(Date dateStart, Date dateEnd)
throws Exception {
String strDiffDateEng = "";
int daysDiff = diffDate(dateStart, dateEnd);
System.out.println("pass 764 - dateStart=" + dateStart.toString()
+ " dateEnd=" + dateEnd.toString());
System.out.println("pass 764 - daysDiff=" + daysDiff);
double dblYearsDiff = daysDiff / 365.25;
int intYearsDiff = (int) (dblYearsDiff);
double dblMonthDiff = (daysDiff % 365.25) / 30.4375;
int intMonthDiff = (int) dblMonthDiff;
double dblDayDiff = (daysDiff % 365.25) % 30.4375;
int intDayDiff = (int) dblDayDiff;
strDiffDateEng = intYearsDiff + " years " + intMonthDiff + " months "
+ intDayDiff + " days ";
return strDiffDateEng;
}
//
// public static int diffDate(Date d1, Date d2) throws Exception{
// int diffDate = 0;
// long diffLong = 0;
// if(d1 != null && d2 != null){
// java.util.GregorianCalendar x1= new java.util.GregorianCalendar();
// x1.setTime(d1);
// java.util.GregorianCalendar x2= new java.util.GregorianCalendar();
// x2.setTime(d2);
//
// long dLong1 =
// x1.getTime().getTime();//x1.UTC(x1.get(x1.YEAR),x1.get(x1.MONTH),x1.get(x1.DATE),x1.get(x1.HOUR_OF_DAY),x1.get(x1.MINUTE),x1.get(x1.SECOND));
// long dLong2 =
// x2.getTime().getTime();//x2.UTC(x2.get(x1.YEAR),x2.get(x1.MONTH),x2.get(x1.DATE),x2.get(x1.HOUR_OF_DAY),x2.get(x1.MINUTE),x2.get(x1.SECOND));
// // if(dLong1>dLong2)
// // diffLong = dLong1 - dLong2;
// // else
// diffLong = dLong2 - dLong1;
//
// diffLong = diffLong / ONEDAY;
// diffDate = (int)diffLong;
//
// }else
// throw new Exception("D1 or D2 is null");
//
// return diffDate;
// }
public static int diff(Date date1, Date date2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
int diffDay = 0;
if (c1.before(c2)) {
diffDay = countDiffDay(c1, c2);
} else {
diffDay = countDiffDay(c2, c1);
}
return diffDay;
}
public static int checkDatediff(Date date1, Date date2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
int diffDay = 0;
if (c1.before(c2)) {
diffDay = diffDate(c2, c1);
} else {
diffDay = 0;
}
return diffDay;
}
public static void DateDiff(Date date1, Date date2) {
int diffDay = diff(date1, date2);
System.out.println("Different Day : " + diffDay);
}
public static int countDiffDay(Calendar c1, Calendar c2) {
int returnInt = 0;
while (!c1.after(c2)) {
c1.add(Calendar.DAY_OF_MONTH, 1);
returnInt++;
}
if (returnInt > 0) {
returnInt = returnInt - 1;
}
return (returnInt);
}
public static int countDiffDay(Date date1, Date date2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
int returnInt = 0;
while (!c1.after(c2)) {
c1.add(Calendar.DAY_OF_MONTH, 1);
returnInt++;
}
if (returnInt > 0) {
returnInt = returnInt - 1;
}
return (returnInt);
}
public static Date makeDate(String dateString) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
return formatter.parse(dateString);
}
public static Date makeDate(String dateString, String pattern)
throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.parse(dateString);
}
public static String convertDateStringFormat(String dateStr,
String formatDateFrom, String formatDateTo) {
return convertDateStringFormat(dateStr, formatDateFrom, null,
formatDateTo, null);
}
public static String convertDateStringFormat(String dateStr,
String formatDateFrom, Locale localeFrom, String formatDateTo,
Locale localeTO) {
// System.out.println("convertDateStringFormat(dateStr<"+ dateStr
// +">)");
if (dateStr == null || "".equals(dateStr)) {
return "";
}
String result = null;
try {
SimpleDateFormat dateFormatFrom = null;
SimpleDateFormat dateFormatTO = null;
if (localeFrom != null) {
dateFormatFrom = new SimpleDateFormat(formatDateFrom,
localeFrom);
} else {
dateFormatFrom = new SimpleDateFormat(formatDateFrom);
}
// dateFormat = new SimpleDateFormat(formatDateFrom);
java.util.Date date = dateFormatFrom.parse(dateStr);
if (localeTO != null) {
dateFormatTO = new SimpleDateFormat(formatDateTo, localeTO);
} else {
dateFormatTO = new SimpleDateFormat(formatDateTo);
}
result = dateFormatTO.format(date);
} catch (Exception e) {
e.printStackTrace();
result = dateStr;
}
return result;
}
public static String convertDateStringFormat(String dateStr, int diffDay,
String formatDateFrom, Locale localeFrom, String formatDateTo,
Locale localeTO) {
// System.out.println("convertDateStringFormat(dateStr<"+ dateStr
// +">)");
if (dateStr == null || "".equals(dateStr)) {
return "";
}
String result = null;
try {
SimpleDateFormat dateFormatFrom = null;
SimpleDateFormat dateFormatTO = null;
if (localeFrom != null) {
dateFormatFrom = new SimpleDateFormat(formatDateFrom,
localeFrom);
} else {
dateFormatFrom = new SimpleDateFormat(formatDateFrom);
}
// dateFormat = new SimpleDateFormat(formatDateFrom);
java.util.Date date = dateFormatFrom.parse(dateStr);
date.setDate(date.getDate() + diffDay);
if (localeTO != null) {
dateFormatTO = new SimpleDateFormat(formatDateTo, localeTO);
} else {
dateFormatTO = new SimpleDateFormat(formatDateTo);
}
result = dateFormatTO.format(date);
} catch (Exception e) {
e.printStackTrace();
result = dateStr;
}
return result;
}
// public static void main(String argv[]) throws Exception {
// Calendar cc1 = Calendar.getInstance();
// Calendar cc2 = Calendar.getInstance();
// cc1.add(Calendar.DAY_OF_MONTH, 10);
//
// DateDiff(cc1.getTime(), cc2.getTime());
//
// java.util.Date d1 = makeDate("10/10/2000");
// java.util.Date d2 = makeDate("10/18/2000");
// DateDiff(d1, d2);
//
// java.util.Date d3 = makeDate("1/1/2000");
// java.util.Date d4 = makeDate("12/31/2000");
// int diff34 = diff(d3, d4);
// System.out.println("diff34=" + diff34);
// }
public static int diffMinute(Calendar first, Calendar last) {
Calendar fCal = DateUtil.getCalendar(DateUtil.getDay(first), DateUtil
.getMonth(first), DateUtil.getYear(first), DateUtil
.getHour(first), DateUtil.getMinute(first), 0);
Calendar lCal = DateUtil.getCalendar(DateUtil.getDay(last), DateUtil
.getMonth(last), DateUtil.getYear(last),
DateUtil.getHour(last), DateUtil.getMinute(last), 0);
long lastLong = lCal.getTime().getTime();
long firstLong = fCal.getTime().getTime();
long diff = lastLong - firstLong;
return (new Long(diff / (60 * 1000))).intValue();
}
public static long diffTimestamp(String first, String last) {
long firstParsedDate = Long.parseLong(first);
long secondParsedDate = Long.parseLong(last);
long diff = secondParsedDate - firstParsedDate;
return diff;
}
public static long diffSecond(String first, String last) throws ParseException {
DateFormat formatter = new SimpleDateFormat("yyyyMMdd:HHmmss");
Date dateFist = formatter.parse(first);
Date dateLast = formatter.parse(last);
Calendar strF_Cal = Calendar.getInstance();
Calendar strL_Cal = Calendar.getInstance();
strF_Cal.setTime(dateFist);
strL_Cal.setTime(dateLast);
Calendar fCal = DateUtil.getCalendar(DateUtil.getDay(strF_Cal), DateUtil
.getMonth(strF_Cal), DateUtil.getYear(strF_Cal), DateUtil
.getHour(strF_Cal), DateUtil.getMinute(strF_Cal), DateUtil.getSecond(strF_Cal));
Calendar lCal = DateUtil.getCalendar(DateUtil.getDay(strL_Cal), DateUtil
.getMonth(strL_Cal), DateUtil.getYear(strL_Cal),
DateUtil.getHour(strL_Cal), DateUtil.getMinute(strL_Cal), DateUtil.getSecond(strL_Cal));
long lastLong = lCal.getTime().getTime();
long firstLong = fCal.getTime().getTime();
long diff = lastLong - firstLong;
return (new Long(diff / 1000));
}
public static java.sql.Date getYesterday(java.sql.Date theDate) {
return diffTomorrowYesterday(theDate, true);
}
public static java.sql.Date getTomorrow(java.sql.Date theDate) {
return diffTomorrowYesterday(theDate, false);
}
private static java.sql.Date diffTomorrowYesterday(java.sql.Date theDate,
boolean theYesterday) {
java.sql.Date w_tmpDate = null;
if (theYesterday) {
w_tmpDate = new java.sql.Date(theDate.getTime() - w_oneDay);
} else {
w_tmpDate = new java.sql.Date(theDate.getTime() + w_oneDay);
}
return w_tmpDate;
}
public static java.util.Date getPreviousOrNextByDayCount(
java.util.Date oldDate, int dayCount) {
java.util.Date newDate = null;
newDate = new java.util.Date(oldDate.getTime() + (w_oneDay * dayCount));
return newDate;
}
public static String convertDateFormatString(java.util.Date oldDate,
String format, Locale locale) {
String newDate = "";
SimpleDateFormat formatter;
formatter = new SimpleDateFormat(format, locale);
newDate = formatter.format(oldDate);
return newDate;
}
public static String convertDateFormatString(java.util.Date oldDate,
int diffDay, String format, Locale locale) {
String newDate = "";
SimpleDateFormat formatter;
formatter = new SimpleDateFormat(format, locale);
oldDate.setDate(oldDate.getDate() + diffDay);
newDate = formatter.format(oldDate);
return newDate;
}
/*--------------------------------*/
/* @author surasc45 - 25 Feb 2009 */
/* Creation for : EBill Project */
/*--------------------------------*/
public synchronized static long getCurrentTime() {
return Calendar.getInstance(new Locale("en", "US")).getTime().getTime();
}
public static java.sql.Date getCurrentDate() {
return new java.sql.Date(getCurrentTime());
}
public static java.sql.Timestamp getCurrentTimestamp() {
return new java.sql.Timestamp(getCurrentTime());
}
public static String convertTimestampToThaiDateString(
java.sql.Timestamp inputDate, String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, new Locale(
"th", "TH"));
String dateStr = formatter.format(inputDate);
return dateStr;
}
public static String convertTimestampToDateString(
java.sql.Timestamp inputDate, String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, new Locale(
"en", "US"));
String dateStr = "";
try {
dateStr = formatter.format(inputDate);
// System.out.println("dateStr : "+ dateStr);
} catch (Exception ex) {
ex.printStackTrace();
}
return dateStr;
}
public static String convertDateToThaiDateString(java.sql.Date inputDate,
String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, new Locale(
"th", "TH"));
String dateStr = formatter.format(inputDate);
return dateStr;
}
public static String convertDateToDateString(java.sql.Date inputDate,
String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, new Locale(
"en", "US"));
String dateStr = "";
try {
dateStr = formatter.format(inputDate);
} catch (Exception ex) {
ex.printStackTrace();
}
return dateStr;
}
public static java.sql.Timestamp stringToTimeStamp(String datestr,
String pattern) {
return stringToTimeStamp(datestr, pattern, new Locale("en", "US"));
}
public static java.sql.Timestamp stringToTimeStamp(String datestr,
String pattern, Locale locale) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
java.util.Date tmpdate = null;
java.sql.Timestamp outdate = null;
try {
tmpdate = formatter.parse(datestr);
outdate = new Timestamp(tmpdate.getTime());
} catch (Exception e) {
System.out.println("Cannot parse String \"" + datestr
+ "\" to TimeStamp object");
}
return outdate;
}
public static java.sql.Date stringToDate(String datestr, String pattern) {
return stringToDate(datestr, pattern, new Locale("en", "US"));
}
public static java.sql.Date stringToDate(String datestr, String pattern,
Locale locale) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
java.util.Date tmpdate = null;
java.sql.Date outdate = null;
try {
tmpdate = formatter.parse(datestr);
outdate = new java.sql.Date(tmpdate.getTime());
} catch (Exception e) {
System.out.println("Cannot parse String \"" + datestr
+ "\" to Date object");
}
return outdate;
}
public static String formatThaiDate(String dateStr, String patternIn,
String patternOut) {
String result = "";
try {
SimpleDateFormat defDateF = new SimpleDateFormat(patternIn,
new Locale("th", "TH"));
java.util.Date date = null;
if (dateStr.equals("")) {
date = new java.util.Date();
} else {
date = defDateF.parse(dateStr);
}
defDateF = new SimpleDateFormat(patternOut, new Locale("th", "TH"));
result = defDateF.format(date);
} catch (Exception e) {
System.out
.println("formatThaiDate with locale : get format date/datetime error.");
e.printStackTrace();
}
return result;
}
public static String formatEngDate(String dateStr, String patternIn,
String patternOut) {
String result = "";
try {
SimpleDateFormat defDateF = new SimpleDateFormat(patternIn,
new Locale("en", "US"));
java.util.Date date = null;
if (dateStr.equals("")) {
date = new java.util.Date();
} else {
date = defDateF.parse(dateStr);
}
defDateF = new SimpleDateFormat(patternOut, new Locale("en", "US"));
result = defDateF.format(date);
} catch (Exception e) {
System.out
.println("formatEngDate with locale : get format date/datetime error.");
e.printStackTrace();
}
return result;
}
public static String formatDate(String dateStr, Locale localeIn,
Locale localeOut) {
String result = "";
try {
SimpleDateFormat defDateF = new SimpleDateFormat("dd/MM/yyyy",
localeIn);
java.util.Date date = null;
if (dateStr.equals("")) {
date = new java.util.Date();
} else {
date = defDateF.parse(dateStr);
}
defDateF = new SimpleDateFormat("dd/MM/yyyy", localeOut);
result = defDateF.format(date);
} catch (Exception e) {
System.out
.println("formatDate with locale : get format date/datetime error.");
e.printStackTrace();
}
return result;
}
public static String formatDateThai(String dateStr, String formatDate,
Locale localeIn, Locale localeOut) {
String result = "";
try {
SimpleDateFormat defDateF = new SimpleDateFormat(formatDate,
localeIn);
java.util.Date date = null;
if (dateStr.equals("")) {
date = new java.util.Date();
} else {
date = defDateF.parse(dateStr);
}
defDateF = new SimpleDateFormat(formatDate, localeOut);
result = defDateF.format(date);
} catch (Exception e) {
System.out
.println("formatDate with locale : get format date/datetime error.");
e.printStackTrace();
}
return result;
}
public static String getMonthThai(int monthNumber) {
String monthNameTH = "";
switch (monthNumber) {
case 1:
monthNameTH = "\u0E21\u0E01\u0E23\u0E32\u0E04\u0E21";
break;
case 2:
monthNameTH = "\u0E01\u0E38\u0E21\u0E20\u0E32\u0E1E\u0E31\u0E19\u0E18\u0E4C";
break;
case 3:
monthNameTH = "\u0E21\u0E35\u0E19\u0E32\u0E04\u0E21";
break;
case 4:
monthNameTH = "\u0E40\u0E21\u0E29\u0E32\u0E22\u0E19";
break;
case 5:
monthNameTH = "\u0E1E\u0E24\u0E29\u0E20\u0E32\u0E04\u0E21";
break;
case 6:
monthNameTH = "\u0E21\u0E34\u0E16\u0E38\u0E19\u0E32\u0E22\u0E19";
break;
case 7:
monthNameTH = "\u0E01\u0E23\u0E01\u0E0F\u0E32\u0E04\u0E21";
break;
case 8:
monthNameTH = "\u0E2A\u0E34\u0E07\u0E2B\u0E32\u0E04\u0E21";
break;
case 9:
monthNameTH = "\u0E01\u0E31\u0E19\u0E22\u0E32\u0E22\u0E19";
break;
case 10:
monthNameTH = "\u0E15\u0E38\u0E25\u0E32\u0E04\u0E21";
break;
case 11:
monthNameTH = "\u0E1E\u0E24\u0E28\u0E08\u0E34\u0E01\u0E32\u0E22\u0E19";
break;
case 12:
monthNameTH = "\u0E18\u0E31\u0E19\u0E27\u0E32\u0E04\u0E21";
break;
}
return monthNameTH;
}
public static String getMonthEng(int monthNumber) {
String monthNameEN = "";
switch (monthNumber) {
case 1:
monthNameEN = "January";
break;
case 2:
monthNameEN = "February";
break;
case 3:
monthNameEN = "March";
break;
case 4:
monthNameEN = "April";
break;
case 5:
monthNameEN = "May";
break;
case 6:
monthNameEN = "June";
break;
case 7:
monthNameEN = "July";
break;
case 8:
monthNameEN = "August";
break;
case 9:
monthNameEN = "September";
break;
case 10:
monthNameEN = "October";
break;
case 11:
monthNameEN = "November";
break;
case 12:
monthNameEN = "December";
break;
}
return monthNameEN;
}
// format "strDate" : mm/dd/yy
public static String dateThai(String strDate) {
String[] date = strDate.split("/");
if (date[0].charAt(0) == '0') {
date[0] = date[0].replaceFirst("0", "");
}
if (date[1].charAt(0) == '0') {
date[1] = date[1].replaceFirst("0", "");
}
return date[1] + " " + getMonthThai(Integer.parseInt(date[0].trim()))
+ " " + date[2].trim();
}
// format "strDate" : mm/dd/yy
public static String dateThaiWithThaiYear(String strDate) {
String[] date = strDate.split("/");
if (date[0].charAt(0) == '0') {
date[0] = date[0].replaceFirst("0", "");
}
if (date[1].charAt(0) == '0') {
date[1] = date[1].replaceFirst("0", "");
}
return date[1] + " " + getMonthThai(Integer.parseInt(date[0].trim()))
+ " " + (Integer.parseInt(date[2].trim()) + 543);
}
// format "strDate" : mm/dd/yy
public static String dateEng(String strDate) {
String[] date = strDate.split("/");
if (date[0].charAt(0) == '0') {
date[0] = date[0].replaceFirst("0", "");
}
if (date[1].charAt(0) == '0') {
date[1] = date[1].replaceFirst("0", "");
}
return date[1] + " " + getMonthEng(Integer.parseInt(date[0].trim()))
+ " " + date[2].trim();
}
// format "strDateTime" : 2009-06-04 18:31:22.0
public static String getDateTimeThai(String strDateTime) {
String strDateTimeThai = "";
if (strDateTime.trim() != "") {
String[] arrayDateTimeThai = strDateTime.split(" ");
String[] arrayDateThai = arrayDateTimeThai[0].split("-");
String[] arrayTimeThai = null;
if (arrayDateTimeThai.length > 1) {
arrayTimeThai = arrayDateTimeThai[1].split(":");
}
strDateTimeThai = arrayDateThai[2].trim()
+ " "
+ DateUtil.getMonthThai(Integer.parseInt(arrayDateThai[1]
.trim())) + " "
+ (Integer.parseInt(arrayDateThai[0].trim()) + 543);
if (arrayTimeThai != null) {
strDateTimeThai += " " + arrayTimeThai[0].trim() + ":"
+ arrayTimeThai[1].trim();
}
}
return strDateTimeThai;
}
// format "strDateTime" : 31/05/2019 14:27:57
public static String getDateTimeThai(String strDateTime, String token) {
String strDateTimeThai = "";
if (strDateTime.trim() != "") {
String[] arrayDateTimeThai = strDateTime.split(" ");
String[] arrayDateThai = arrayDateTimeThai[0].split(token);
strDateTimeThai = arrayDateThai[0].trim()
+ " "
+ DateUtil.getMonthThai(Integer.parseInt(arrayDateThai[1]
.trim())) + " "
+ (Integer.parseInt(arrayDateThai[2].trim()) + 543);
strDateTimeThai += " " + arrayDateTimeThai[1];
}
return strDateTimeThai;
}
// format "strDateTime" : 31/05/2019 14:27:57
public static String getDateTimeEng(String strDateTime, String token) {
String strDateTimeEng = "";
if (strDateTime.trim() != "") {
String[] arrayDateTimeEng = strDateTime.split(" ");
String[] arrayDateThai = arrayDateTimeEng[0].split(token);
strDateTimeEng = arrayDateThai[0].trim()
+ " "
+ DateUtil.getMonthEng(Integer.parseInt(arrayDateThai[1]
.trim())) + " "
+ (Integer.parseInt(arrayDateThai[2].trim()));
strDateTimeEng += " " + arrayDateTimeEng[1];
}
return strDateTimeEng;
}
// format "strDateTime" : 2009-06-04 18:31:22.0
public static String getDateTimeEng(String strDateTime) {
String strDateTimeEng = "";
if (strDateTime.trim() != "") {
String[] arrayDateTimeEng = strDateTime.split(" ");
String[] arrayDateEng = arrayDateTimeEng[0].split("-");
String[] arrayTimeEng = null;
if (arrayDateTimeEng.length > 1) {
arrayTimeEng = arrayDateTimeEng[1].split(":");
}
strDateTimeEng = arrayDateEng[2].trim()
+ " "
+ DateUtil.getMonthEng(Integer.parseInt(arrayDateEng[1]
.trim())) + " "
+ (Integer.parseInt(arrayDateEng[0].trim()));
if (arrayTimeEng != null) {
strDateTimeEng += " " + arrayTimeEng[0].trim() + ":"
+ arrayTimeEng[1].trim();
}
}
return strDateTimeEng;
}
// format "strDateTime" : 2009-06-04 18:31:22.0
/**
* @param strDateTime
* 2009-06-04 18:31:22.0
* @return 04/06/2009 18:31:22
*/
public static String getDateTimeString(String strDateTime) {
String strDateTimeEng = "";
if (strDateTime.trim() != "") {
String[] arrayDateTimeEng = strDateTime.split(" ");
String[] arrayDateEng = arrayDateTimeEng[0].split("-");
String[] arrayTimeEng = null;
if (arrayDateTimeEng.length > 1) {
arrayTimeEng = arrayDateTimeEng[1].split(":");
}
strDateTimeEng = arrayDateEng[2].trim() + "/"
+ arrayDateEng[1].trim() + "/"
+ (Integer.parseInt(arrayDateEng[0].trim()));
if (arrayTimeEng != null) {
strDateTimeEng += " " + arrayTimeEng[0].trim() + ":"
+ arrayTimeEng[1].trim();
}
}
return strDateTimeEng;
}
public static Calendar getCalendar(Locale locale) {
Calendar cal = Calendar.getInstance(locale);
cal.setTimeZone(TimeZone.getTimeZone("GMT+07:00"));
return cal;
}
public static Calendar getSimpleDate(String strDate,String simpleDateFormat, Locale locale) {
Calendar calendar = null;
if (strDate != null && strDate.trim().length() > 0) {
try {
calendar = getCalendar(Locale.US);
calendar.setTime(new SimpleDateFormat(simpleDateFormat, locale).parse(strDate));
} catch (Exception e) {
calendar = null;
}
}
return calendar;
}
public static int getCustomerAge(String birthDate,String pattern,String locale) {
return getCustomerAge(birthDate,pattern,new Locale(locale));
}
public static int getCustomerAge(String birthDate,String pattern,Locale locale) {
Calendar dob = getSimpleDate(birthDate, pattern, locale);
Calendar now = Calendar.getInstance(locale);
if (dob.after(now)) {
throw new IllegalArgumentException("Can't be born in the future");
}
int year1 = now.get(Calendar.YEAR);
int year2 = dob.get(Calendar.YEAR);
int age = year1 - year2;
int month1 = now.get(Calendar.MONTH);
int month2 = dob.get(Calendar.MONTH);
if (month2 > month1) {
age--;
} else if (month1 == month2) {
int day1 = now.get(Calendar.DAY_OF_MONTH);
int day2 = dob.get(Calendar.DAY_OF_MONTH);
if (day2 > day1) {
age--;
}
}
return age;
}
public static String mappingMonth(HashMap monthMap, String date) {
String result = "";
try {
if (date != null && !"".equals(date)) {
String month = date.substring(3, 6);
// System.out.println("sub month : "+ month);
result = date.replaceAll(month, String.valueOf(monthMap
.get(month)));
// System.out.println("replace month to : "+ result);
} else {
return "";
}
} catch (Exception e) {
e.printStackTrace();
result = date;
}
return result;
}
public static HashMap getMonthHashMap() {
HashMap monthMap = new HashMap();
monthMap.put("JAN", "01");
monthMap.put("FEB", "02");
monthMap.put("MAR", "03");
monthMap.put("APR", "04");
monthMap.put("MAY", "05");
monthMap.put("JUN", "06");
monthMap.put("JUL", "07");
monthMap.put("AUG", "08");
monthMap.put("SEP", "09");
monthMap.put("OCT", "10");
monthMap.put("NOV", "11");
monthMap.put("DEC", "12");
return monthMap;
}
public static String getMonthStr(int calendarMonth) {
String rtnMonth = "";
switch (calendarMonth) {
case 0: rtnMonth = "01";
break;
case 1: rtnMonth = "02";
break;
case 2: rtnMonth = "03";
break;
case 3: rtnMonth = "04";
break;
case 4: rtnMonth = "05";
break;
case 5: rtnMonth = "06";
break;
case 6: rtnMonth = "07";
break;
case 7: rtnMonth = "08";
break;
case 8: rtnMonth = "09";
break;
case 9: rtnMonth = "10";
break;
case 10:rtnMonth = "11";
break;
case 11:rtnMonth = "12";
break;
default:
rtnMonth = "01";
break;
}
return rtnMonth;
}
public static String getFirstDay(Date d) throws Exception {
Calendar calendar = new GregorianCalendar(Locale.US);
calendar.setTime(d);
calendar.set(Calendar.DAY_OF_MONTH, 1);
Date dddd = calendar.getTime();
SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
return sdf1.format(dddd);
}
public static Calendar getLastDay(Date d) throws Exception {
Calendar calendar = new GregorianCalendar(Locale.US);
calendar.setTime(d);
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMaximum(Calendar.DAY_OF_MONTH));
// Date dddd = calendar.getInstance();
return calendar;
}
public static String getMonthEng_Abbreviations(String month) {
String monthNameEN = "";
if (month.equals("JAN")) {
monthNameEN = "01";
} else if (month.equals("FEB")) {
monthNameEN = "02";
} else if (month.equals("MAR")) {
monthNameEN = "03";
} else if (month.equals("APR")) {
monthNameEN = "04";
} else if (month.equals("MAY")) {
monthNameEN = "05";
} else if (month.equals("JUN")) {
monthNameEN = "06";
} else if (month.equals("JUL")) {
monthNameEN = "07";
} else if (month.equals("AUG")) {
monthNameEN = "08";
} else if (month.equals("SEP")) {
monthNameEN = "09";
} else if (month.equals("OCT")) {
monthNameEN = "10";
} else if (month.equals("NOV")) {
monthNameEN = "11";
} else if (month.equals("DEC")) {
monthNameEN = "12";
}
return monthNameEN;
}
/*
* #tom
*
* gen TransactionId
*/
public static String getSONbr()
{
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
int randomNum1 = new Random().nextInt((1000000 - 100000) + 1) + 100000;
int randomNum2 = new Random().nextInt((1000000 - 100000) + 1) + 100000;
int randomNum3 = new Random().nextInt((10000 - 1000) + 1) + 1000;
String output = dateFormat.format(cal.getTime()) + randomNum1 + randomNum2 + randomNum3;
// System.out.println(output);
return output;
}
/**
*
* @param dateStr
* format MM/DD/YYYY HH:MM:SS
* @return
*/
public static Calendar getCalendar(String dateStr) {
// DateFormat format = DateFormat.getInstance("MM/DD/YYYY HH:MM:SS");
Calendar calendar = null;
try {
SimpleDateFormat format = new SimpleDateFormat(
"MM/dd/yyyy HH:mm:ss", Locale.US);
calendar = Calendar.getInstance();
calendar.setTime(format.parse(dateStr));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return calendar;
}
public static String dateTimeThaiddMMNoTime(String strDate){
Date tempDate = DateUtil.stringToDate(strDate, "dd/MM/yyyy");
String[] tempSplit = strDate.split(" ");
String tempTime = tempSplit[1]==null?"":tempSplit[1];
String strFullDate = tempDate.getDate() + " " + getMonthThai(tempDate.getMonth()+1) + " " + (tempDate.getYear() +2443);
return strFullDate;
}
public static String dateTimeEngddMMNoTime(String strDate){
Date tempDate = DateUtil.stringToDate(strDate, "dd/MM/yyyy");
String[] tempSplit = strDate.split(" ");
String tempTime = tempSplit[1]==null?"":tempSplit[1];
String strFullDate = tempDate.getDate() + " " + getMonthEng(tempDate.getMonth()+1) + " " + (tempDate.getYear() +1900);
return strFullDate;
}
public static String dateTimeThaiddMM(String strDate){
Date tempDate = DateUtil.stringToDate(strDate, "dd/MM/yyyy");
String[] tempSplit = strDate.split(" ");
String tempTime = tempSplit[1]==null?"":tempSplit[1];
String strFullDate = tempDate.getDate() + " " + getMonthThai(tempDate.getMonth()+1) + " " + (tempDate.getYear() +2443) + " " + tempTime;
return strFullDate;
}
public static String dateTimeEngddMM(String strDate){
Date tempDate = DateUtil.stringToDate(strDate, "dd/MM/yyyy");
String[] tempSplit = strDate.split(" ");
String tempTime = tempSplit[1]==null?"":tempSplit[1];
String strFullDate = tempDate.getDate() + " " + getMonthEng(tempDate.getMonth()+1) + " " + (tempDate.getYear() +1900) + " " + tempTime;
return strFullDate;
}
public static String checkWithIn3MonthsAgo(){
Timestamp startTime = DateUtil.getCurrentTimestamp();
String curDate = DateUtil.convertTimestampToDateString(startTime, "dd/MM/yyyy");
String[] arrCurDate = curDate.split("/");
// String[] arrCurDate = "02/01/2012".split("/");
String result = "";
String tempMonth = "";
for(int i = 0; i < 4; i++){
if((Integer.parseInt(arrCurDate[1])-i) > 0){ // this year
tempMonth = (Integer.parseInt(arrCurDate[1])-i)+"";
if(tempMonth.length() == 1){
tempMonth = "0" + tempMonth;
}
result += (tempMonth+"/") + (arrCurDate[2]+"|");
}else{ // last year
if((Integer.parseInt(arrCurDate[1])-i) == 0){
result += 12+"/";
}else if((Integer.parseInt(arrCurDate[1])-i) == -1){
result += 11+"/";
}else if((Integer.parseInt(arrCurDate[1])-i) == -2){
result += 10+"/";
}else if((Integer.parseInt(arrCurDate[1])-i) == -3){
result += "0"+9+"/";
}
result += ((Integer.parseInt(arrCurDate[2])-1)+"|");
}
}
return result;
}
public static String dateWithThaiYear2(String strDate){
String[] temp = strDate.split("/");
if(temp[0].length() == 1){
temp[0] = "0" + temp[0];
}
if(temp[1].length() == 1){
temp[1] = "0" + temp[1];
}
return temp[0].trim() + "/" + temp[1].trim() + "/" + (Integer.parseInt(temp[2].trim()) +543);
}
public static String getDateTimeThaiContractSFF(String strDateTime){
String strDateTimeThai = "";
System.out.println("lerk strDateTime = "+strDateTime);
if (strDateTime.trim() != ""){
String[] arrayDateThai = strDateTime.split("/");
if(arrayDateThai.length == 3){
System.out.println("Length of arrayDateThai = 3");
String Year = arrayDateThai[2].substring(0,4);
System.out.println("lerk Year = "+Year);
strDateTimeThai = arrayDateThai[0].trim() + " " + DateUtil.getMonthThai(Integer.parseInt(arrayDateThai[1].trim())) + " " + (Integer.parseInt(Year.trim())+543) ;
System.out.println("strDateTimeThai = "+strDateTimeThai);
}
else
{
System.out.println("But Length of arrayDateThai = "+arrayDateThai.length);
}
}
return strDateTimeThai;
}
// dd/MM/yyyy
public static String getDateTimeEngContractSFF(String strDateTime){
String strDateTimeEng = "";
if (strDateTime.trim() != ""){
String[] arrayDateEng = strDateTime.split("/");
if(arrayDateEng.length == 3){
System.out.println("Length of arrayDateEng = 3");
String Year = arrayDateEng[2].substring(0,4);
strDateTimeEng = arrayDateEng[0].trim() + " " + DateUtil.getMonthEng(Integer.parseInt(arrayDateEng[1].trim())) + " " + (Integer.parseInt(Year.trim()));
}
else
{
System.out.println("But Length of arrayDateEng = "+arrayDateEng.length);
}
}
return strDateTimeEng;
}
public static String convertStringDateTimeToStringFormat(String date){
String dateDt="";
try{
DateFormat formatter;
Date dateNew;
formatter=new SimpleDateFormat("ddMMyyyy HH:mm:ss",new Locale("en","US"));
dateNew=(Date)formatter.parse(date);
formatter=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",new Locale("en","US"));
//dateDt=dateWithThaiYear(formatter.format(dateNew));
dateDt=formatter.format(dateNew);
String date1[]= dateDt.split(" ");
int str1=0;
if(date1.length>=2){
String date2[]=date1[0].split("/");
if(date2.length>=3){
str1=Integer.parseInt(date2[2])+543;
}
dateDt=date2[0]+"/"+date2[1]+"/"+str1+" "+date1[1];
}
}catch(ParseException e){
e.printStackTrace();
}
return dateDt;
}
public static String convertStringDateTimeToStringFormatEn(String date){
String dateDt="";
try{
DateFormat formatter;
Date dateNew;
formatter=new SimpleDateFormat("ddMMyyyy HH:mm:ss",new Locale("en","US"));
dateNew=(Date)formatter.parse(date);
formatter=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",new Locale("en","US"));
//dateDt=dateWithThaiYear(formatter.format(dateNew));
dateDt=formatter.format(dateNew);
}catch(ParseException e){
e.printStackTrace();
}
return dateDt;
}
public static String convertStringTimeStampToStringFormat(String date){
String dateDt="";
try{
DateFormat formatter;
Date dateNew;
formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",new Locale("en","US"));
dateNew=(Date)formatter.parse(date);
formatter=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",new Locale("en","US"));
//dateDt=dateWithThaiYear(formatter.format(dateNew));
dateDt=formatter.format(dateNew);
String date1[]= dateDt.split(" ");
int str1=0;
if(date1.length>=2){
String date2[]=date1[0].split("/");
if(date2.length>=3){
str1=Integer.parseInt(date2[2])+543;
}
dateDt=date2[0]+"/"+date2[1]+"/"+str1+" "+date1[1];
}
}catch(ParseException e){
e.printStackTrace();
}
return dateDt;
}
public static String convertStringTimeStampToStringFormatEn(String date){
String dateDt="";
try{
DateFormat formatter;
Date dateNew;
formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",new Locale("en","US"));
dateNew=(Date)formatter.parse(date);
formatter=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",new Locale("en","US"));
//dateDt=dateWithThaiYear(formatter.format(dateNew));
dateDt=formatter.format(dateNew);
}catch(ParseException e){
e.printStackTrace();
}
return dateDt;
}
public static String dateTimeThai(String strDate){
Date tempDate = DateUtil.stringToDate(strDate, "MM/dd/yyyy HH:mm:ss");
String[] tempSplit = strDate.split(" ");
String tempTime = tempSplit[1]==null?"":tempSplit[1];
String strFullDate = tempDate.getDate() + " " + getMonthThai(tempDate.getMonth()+1) + " " + (tempDate.getYear() +2443) + " " + tempTime;
return strFullDate;
}
public static String dateTimeEng(String strDate){
Date tempDate = DateUtil.stringToDate(strDate, "MM/dd/yyyy HH:mm:ss");
String[] tempSplit = strDate.split(" ");
String tempTime = tempSplit[1]==null?"":tempSplit[1];
String strFullDate = tempDate.getDate() + " " + getMonthEng(tempDate.getMonth()+1) + " " + (tempDate.getYear() +1900) + " " + tempTime;
return strFullDate;
}
public static int countDay(Date startDate, Date endDate) throws NullPointerException {
if(endDate==null)
throw new NullPointerException("DateUtil.countDay().endDate:null");
if(startDate==null)
throw new NullPointerException("DateUtil.countDay().startDate:null");
return (int)( (startDate.getTime() - endDate.getTime()) / (86400000) )+1;
}
public static boolean betweenCurrentTime(String start,String end,String pattern) throws ParseException{
Calendar current = DateUtil.getCurrentDateTime();
SimpleDateFormat sf = new SimpleDateFormat(pattern);
String currentStr = sf.format(current.getTime());
Date currentTime = sf.parse(currentStr);
Date startTime = sf.parse(start);
Date endTime = sf.parse(end);
current.setTime(currentTime);
Calendar startCalendar = DateUtil.getCurrentDateTime();
startCalendar.setTime(startTime);
Calendar endCalendar = DateUtil.getCurrentDateTime();
endCalendar.setTime(endTime);
if(startCalendar.compareTo(endCalendar)<=0)
return current.compareTo(startCalendar)>=0&¤t.compareTo(endCalendar)<=0;
else
return current.compareTo(startCalendar)>=0||current.compareTo(endCalendar)<=0;
}
/**
* calculates the last date of a month
*/
public static Integer calculateMonthEndDate(int month, int year) {
int[] daysInAMonth = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = daysInAMonth[month];
boolean isLeapYear = new GregorianCalendar().isLeapYear(year);
if (isLeapYear && month == 2) {
day++;
}
GregorianCalendar gc = new GregorianCalendar(year, month - 1, day);
java.sql.Date monthEndDate = new java.sql.Date(gc.getTime().getTime());
return monthEndDate.getDate();
}
public static boolean checkBillCycle(String lastInvStartDateStr, String lastInvEndDateStr, String billCycle) {
boolean result = false;
try {
int lastInvStartDate = Integer.parseInt(lastInvStartDateStr.split("/")[0].trim());
int lastInvEndDate = Integer.parseInt(lastInvEndDateStr.split("/")[0].trim());
int calculatedDate = calculateMonthEndDate(Integer.parseInt(lastInvEndDateStr.split("/")[1].trim()), Integer.parseInt(lastInvEndDateStr.split("/")[2].trim()));
String billCycleSelect = billCycle!=null?billCycle:"";
String cycleType = billCycleSelect;
cycleType = cycleType.replace("Cycle ", "");
String[] temp = cycleType.split("/");
int cycleStartDay = Integer.parseInt( ((String)temp[0]).split("-")[0].toString() );
int cycleEndDay = Integer.parseInt( ((String)temp[0]).split("-")[1].toString() );
DateFormat df = new SimpleDateFormat ("dd/MM/yyyy");
Date d1 = df.parse(lastInvStartDateStr);
Date d2 = df.parse(lastInvEndDateStr);
String checkLastInv = String.valueOf(lastInvStartDate)+"-"+String.valueOf(lastInvEndDate);
String checkCycleBill = String.valueOf(cycleStartDay)+"-"+String.valueOf(cycleEndDay);
if(!d1.after(d2)){
if((!checkLastInv.equals(checkCycleBill)) && (!checkCycleBill.equals("1-30")) ){
return result = false;
}else{
if( (lastInvStartDate==cycleStartDay)&&(lastInvEndDate==cycleEndDay) ){
if(checkCycleBill.equals("1-30") ){
if(calculatedDate == cycleEndDay){
return result = true;
}else{
return result = false;
}
}else{
return result = true;
}
}
if((lastInvStartDate==cycleStartDay)&&(lastInvEndDate==calculatedDate) ){
result = true;
}else{
result = false;
}
}
}else{
result = false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
}