1 16 19 20 package org.apache.xalan.lib; 21 22 23 import java.text.ParseException ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Calendar ; 26 import java.util.Date ; 27 import java.util.TimeZone ; 28 29 import org.apache.xpath.objects.XBoolean; 30 import org.apache.xpath.objects.XNumber; 31 import org.apache.xpath.objects.XObject; 32 33 46 47 public class ExsltDatetime 48 { 49 static final String dt = "yyyy-MM-dd'T'HH:mm:ss"; 51 static final String d = "yyyy-MM-dd"; 52 static final String gym = "yyyy-MM"; 53 static final String gy = "yyyy"; 54 static final String gmd = "--MM-dd"; 55 static final String gm = "--MM--"; 56 static final String gd = "---dd"; 57 static final String t = "HH:mm:ss"; 58 static final String EMPTY_STR = ""; 59 60 73 public static String dateTime() 74 { 75 Calendar cal = Calendar.getInstance(); 76 Date datetime = cal.getTime(); 77 SimpleDateFormat dateFormat = new SimpleDateFormat (dt); 79 80 StringBuffer buff = new StringBuffer (dateFormat.format(datetime)); 81 int offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET); 84 if (offset == 0) 87 buff.append("Z"); 88 else 89 { 90 int hrs = offset/(60*60*1000); 92 int min = offset%(60*60*1000); 94 char posneg = hrs < 0? '-': '+'; 95 buff.append(posneg + formatDigits(hrs) + ':' + formatDigits(min)); 96 } 97 return buff.toString(); 98 } 99 100 105 private static String formatDigits(int q) 106 { 107 String dd = String.valueOf(Math.abs(q)); 108 return dd.length() == 1 ? '0' + dd : dd; 109 } 110 111 132 public static String date(String datetimeIn) 133 throws ParseException 134 { 135 String [] edz = getEraDatetimeZone(datetimeIn); 136 String leader = edz[0]; 137 String datetime = edz[1]; 138 String zone = edz[2]; 139 if (datetime == null || zone == null) 140 return EMPTY_STR; 141 142 String [] formatsIn = {dt, d}; 143 String formatOut = d; 144 Date date = testFormats(datetime, formatsIn); 145 if (date == null) return EMPTY_STR; 146 147 SimpleDateFormat dateFormat = new SimpleDateFormat (formatOut); 148 dateFormat.setLenient(false); 149 String dateOut = dateFormat.format(date); 150 if (dateOut.length() == 0) 151 return EMPTY_STR; 152 else 153 return (leader + dateOut + zone); 154 } 155 156 157 160 public static String date() 161 { 162 String datetime = dateTime().toString(); 163 String date = datetime.substring(0, datetime.indexOf("T")); 164 String zone = datetime.substring(getZoneStart(datetime)); 165 return (date + zone); 166 } 167 168 190 public static String time(String timeIn) 191 throws ParseException 192 { 193 String [] edz = getEraDatetimeZone(timeIn); 194 String time = edz[1]; 195 String zone = edz[2]; 196 if (time == null || zone == null) 197 return EMPTY_STR; 198 199 String [] formatsIn = {dt, d}; 200 String formatOut = t; 201 Date date = testFormats(time, formatsIn); 202 if (date == null) return EMPTY_STR; 203 SimpleDateFormat dateFormat = new SimpleDateFormat (formatOut); 204 String out = dateFormat.format(date); 205 return (out + zone); 206 } 207 208 211 public static String time() 212 { 213 String datetime = dateTime().toString(); 214 String time = datetime.substring(datetime.indexOf("T")+1); 215 String zone = datetime.substring(getZoneStart(datetime)); 216 return (time + zone); 217 } 218 219 234 public static double year(String datetimeIn) 235 throws ParseException 236 { 237 String [] edz = getEraDatetimeZone(datetimeIn); 238 boolean ad = edz[0].length() == 0; String datetime = edz[1]; 240 if (datetime == null) 241 return Double.NaN; 242 243 String [] formats = {dt, d, gym, gy}; 244 double yr = getNumber(datetime, formats, Calendar.YEAR); 245 if (ad || yr == Double.NaN) 246 return yr; 247 else 248 return -yr; 249 } 250 251 254 public static double year() 255 { 256 Calendar cal = Calendar.getInstance(); 257 return cal.get(Calendar.YEAR); 258 } 259 260 276 public static double monthInYear(String datetimeIn) 277 throws ParseException 278 { 279 String [] edz = getEraDatetimeZone(datetimeIn); 280 String datetime = edz[1]; 281 if (datetime == null) 282 return Double.NaN; 283 284 String [] formats = {dt, d, gym, gm, gmd}; 285 return getNumber(datetime, formats, Calendar.MONTH); 286 } 287 288 291 public static double monthInYear() 292 { 293 Calendar cal = Calendar.getInstance(); 294 return cal.get(Calendar.MONTH); 295 } 296 297 310 public static double weekInYear(String datetimeIn) 311 throws ParseException 312 { 313 String [] edz = getEraDatetimeZone(datetimeIn); 314 String datetime = edz[1]; 315 if (datetime == null) 316 return Double.NaN; 317 318 String [] formats = {dt, d}; 319 return getNumber(datetime, formats, Calendar.WEEK_OF_YEAR); 320 } 321 322 325 public static double weekInYear() 326 { 327 Calendar cal = Calendar.getInstance(); 328 return cal.get(Calendar.WEEK_OF_YEAR); 329 } 330 331 344 public static double dayInYear(String datetimeIn) 345 throws ParseException 346 { 347 String [] edz = getEraDatetimeZone(datetimeIn); 348 String datetime = edz[1]; 349 if (datetime == null) 350 return Double.NaN; 351 352 String [] formats = {dt, d}; 353 return getNumber(datetime, formats, Calendar.DAY_OF_YEAR); 354 } 355 356 359 public static double dayInYear() 360 { 361 Calendar cal = Calendar.getInstance(); 362 return cal.get(Calendar.DAY_OF_YEAR); 363 } 364 365 366 381 public static double dayInMonth(String datetimeIn) 382 throws ParseException 383 { 384 String [] edz = getEraDatetimeZone(datetimeIn); 385 String datetime = edz[1]; 386 String [] formats = {dt, d, gmd, gd}; 387 double day = getNumber(datetime, formats, Calendar.DAY_OF_MONTH); 388 return day; 389 } 390 391 394 public static double dayInMonth() 395 { 396 Calendar cal = Calendar.getInstance(); 397 return cal.get(Calendar.DAY_OF_MONTH); 398 } 399 400 414 public static double dayOfWeekInMonth(String datetimeIn) 415 throws ParseException 416 { 417 String [] edz = getEraDatetimeZone(datetimeIn); 418 String datetime = edz[1]; 419 if (datetime == null) 420 return Double.NaN; 421 422 String [] formats = {dt, d}; 423 return getNumber(datetime, formats, Calendar.DAY_OF_WEEK_IN_MONTH); 424 } 425 426 429 public static double dayOfWeekInMonth() 430 { 431 Calendar cal = Calendar.getInstance(); 432 return cal.get(Calendar.DAY_OF_WEEK_IN_MONTH); 433 } 434 435 436 450 public static double dayInWeek(String datetimeIn) 451 throws ParseException 452 { 453 String [] edz = getEraDatetimeZone(datetimeIn); 454 String datetime = edz[1]; 455 if (datetime == null) 456 return Double.NaN; 457 458 String [] formats = {dt, d}; 459 return getNumber(datetime, formats, Calendar.DAY_OF_WEEK); 460 } 461 462 465 public static double dayInWeek() 466 { 467 Calendar cal = Calendar.getInstance(); 468 return cal.get(Calendar.DAY_OF_WEEK); 469 } 470 471 484 public static double hourInDay(String datetimeIn) 485 throws ParseException 486 { 487 String [] edz = getEraDatetimeZone(datetimeIn); 488 String datetime = edz[1]; 489 if (datetime == null) 490 return Double.NaN; 491 492 String [] formats = {dt, t}; 493 return getNumber(datetime, formats, Calendar.HOUR_OF_DAY); 494 } 495 496 499 public static double hourInDay() 500 { 501 Calendar cal = Calendar.getInstance(); 502 return cal.get(Calendar.HOUR_OF_DAY); 503 } 504 505 518 public static double minuteInHour(String datetimeIn) 519 throws ParseException 520 { 521 String [] edz = getEraDatetimeZone(datetimeIn); 522 String datetime = edz[1]; 523 if (datetime == null) 524 return Double.NaN; 525 526 String [] formats = {dt,t}; 527 return getNumber(datetime, formats, Calendar.MINUTE); 528 } 529 530 533 public static double minuteInHour() 534 { 535 Calendar cal = Calendar.getInstance(); 536 return cal.get(Calendar.MINUTE); 537 } 538 539 552 public static double secondInMinute(String datetimeIn) 553 throws ParseException 554 { 555 String [] edz = getEraDatetimeZone(datetimeIn); 556 String datetime = edz[1]; 557 if (datetime == null) 558 return Double.NaN; 559 560 String [] formats = {dt, t}; 561 return getNumber(datetime, formats, Calendar.SECOND); 562 } 563 564 567 public static double secondInMinute() 568 { 569 Calendar cal = Calendar.getInstance(); 570 return cal.get(Calendar.SECOND); 571 } 572 573 588 public static XObject leapYear(String datetimeIn) 589 throws ParseException 590 { 591 String [] edz = getEraDatetimeZone(datetimeIn); 592 String datetime = edz[1]; 593 if (datetime == null) 594 return new XNumber(Double.NaN); 595 596 String [] formats = {dt, d, gym, gy}; 597 double dbl = getNumber(datetime, formats, Calendar.YEAR); 598 if (dbl == Double.NaN) 599 return new XNumber(Double.NaN); 600 int yr = (int)dbl; 601 return new XBoolean(yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0)); 602 } 603 604 607 public static boolean leapYear() 608 { 609 Calendar cal = Calendar.getInstance(); 610 int yr = (int)cal.get(Calendar.YEAR); 611 return (yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0)); 612 } 613 614 633 public static String monthName(String datetimeIn) 634 throws ParseException 635 { 636 String [] edz = getEraDatetimeZone(datetimeIn); 637 String datetime = edz[1]; 638 if (datetime == null) 639 return EMPTY_STR; 640 641 String [] formatsIn = {dt, d, gym, gm}; 642 String formatOut = "MMMM"; 643 return getNameOrAbbrev(datetimeIn, formatsIn, formatOut); 644 } 645 646 649 public static String monthName() 650 { 651 Calendar cal = Calendar.getInstance(); 652 String format = "MMMM"; 653 return getNameOrAbbrev(format); 654 } 655 656 676 public static String monthAbbreviation(String datetimeIn) 677 throws ParseException 678 { 679 String [] edz = getEraDatetimeZone(datetimeIn); 680 String datetime = edz[1]; 681 if (datetime == null) 682 return EMPTY_STR; 683 684 String [] formatsIn = {dt, d, gym, gm}; 685 String formatOut = "MMM"; 686 return getNameOrAbbrev(datetimeIn, formatsIn, formatOut); 687 } 688 689 692 public static String monthAbbreviation() 693 { 694 String format = "MMM"; 695 return getNameOrAbbrev(format); 696 } 697 698 716 public static String dayName(String datetimeIn) 717 throws ParseException 718 { 719 String [] edz = getEraDatetimeZone(datetimeIn); 720 String datetime = edz[1]; 721 if (datetime == null) 722 return EMPTY_STR; 723 724 String [] formatsIn = {dt, d}; 725 String formatOut = "EEEE"; 726 return getNameOrAbbrev(datetimeIn, formatsIn, formatOut); 727 } 728 729 732 public static String dayName() 733 { 734 String format = "EEEE"; 735 return getNameOrAbbrev(format); 736 } 737 738 756 public static String dayAbbreviation(String datetimeIn) 757 throws ParseException 758 { 759 String [] edz = getEraDatetimeZone(datetimeIn); 760 String datetime = edz[1]; 761 if (datetime == null) 762 return EMPTY_STR; 763 764 String [] formatsIn = {dt, d}; 765 String formatOut = "EEE"; 766 return getNameOrAbbrev(datetimeIn, formatsIn, formatOut); 767 } 768 769 772 public static String dayAbbreviation() 773 { 774 String format = "EEE"; 775 return getNameOrAbbrev(format); 776 } 777 778 783 private static String [] getEraDatetimeZone(String in) 784 { 785 String leader = ""; 786 String datetime = in; 787 String zone = ""; 788 if (in.charAt(0)=='-' && !in.startsWith("--")) 789 { 790 leader = "-"; datetime = in.substring(1); 792 } 793 int z = getZoneStart(datetime); 794 if (z > 0) 795 { 796 zone = datetime.substring(z); 797 datetime = datetime.substring(0, z); 798 } 799 else if (z == -2) 800 zone = null; 801 return new String []{leader, datetime, zone}; 803 } 804 805 811 private static int getZoneStart (String datetime) 812 { 813 if (datetime.indexOf("Z") == datetime.length()-1) 814 return datetime.length()-1; 815 else if (datetime.length() >=6 816 && datetime.charAt(datetime.length()-3) == ':' 817 && (datetime.charAt(datetime.length()-6) == '+' 818 || datetime.charAt(datetime.length()-6) == '-')) 819 { 820 try 821 { 822 SimpleDateFormat dateFormat = new SimpleDateFormat ("HH:mm"); 823 dateFormat.setLenient(false); 824 Date d = dateFormat.parse(datetime.substring(datetime.length() -5)); 825 return datetime.length()-6; 826 } 827 catch (ParseException pe) 828 { 829 System.out.println("ParseException " + pe.getErrorOffset()); 830 return -2; } 832 833 } 834 return -1; } 836 837 841 private static Date testFormats (String in, String [] formats) 842 throws ParseException 843 { 844 for (int i = 0; i <formats.length; i++) 845 { 846 try 847 { 848 SimpleDateFormat dateFormat = new SimpleDateFormat (formats[i]); 849 dateFormat.setLenient(false); 850 return dateFormat.parse(in); 851 } 852 catch (ParseException pe) 853 { 854 } 855 } 856 return null; 857 } 858 859 860 864 private static double getNumber(String in, String [] formats, int calField) 865 throws ParseException 866 { 867 Calendar cal = Calendar.getInstance(); 868 cal.setLenient(false); 869 Date date = testFormats(in, formats); 871 if (date == null) return Double.NaN; 872 cal.setTime(date); 873 return cal.get(calField); 874 } 875 876 879 private static String getNameOrAbbrev(String in, 880 String [] formatsIn, 881 String formatOut) 882 throws ParseException 883 { 884 for (int i = 0; i <formatsIn.length; i++) { 886 try 887 { 888 SimpleDateFormat dateFormat = new SimpleDateFormat (formatsIn[i]); 889 dateFormat.setLenient(false); 890 Date dt = dateFormat.parse(in); 891 dateFormat.applyPattern(formatOut); 892 return dateFormat.format(dt); 893 } 894 catch (ParseException pe) 895 { 896 } 897 } 898 return ""; 899 } 900 904 private static String getNameOrAbbrev(String format) 905 { 906 Calendar cal = Calendar.getInstance(); 907 SimpleDateFormat dateFormat = new SimpleDateFormat (format); 908 return dateFormat.format(cal.getTime()); 909 } 910 911 947 public static String formatDate(String dateTime, String pattern) 948 { 949 final String yearSymbols = "Gy"; 950 final String monthSymbols = "M"; 951 final String daySymbols = "dDEFwW"; 952 TimeZone timeZone; 953 String zone; 954 955 if (dateTime.endsWith("Z") || dateTime.endsWith("z")) 958 { 959 timeZone = TimeZone.getTimeZone("GMT"); 960 dateTime = dateTime.substring(0, dateTime.length()-1) + "GMT"; 961 zone = "z"; 962 } 963 else if ((dateTime.length() >= 6) 964 && (dateTime.charAt(dateTime.length()-3) == ':') 965 && ((dateTime.charAt(dateTime.length()-6) == '+') 966 || (dateTime.charAt(dateTime.length()-6) == '-'))) 967 { 968 String offset = dateTime.substring(dateTime.length()-6); 969 970 if ("+00:00".equals(offset) || "-00:00".equals(offset)) 971 { 972 timeZone = TimeZone.getTimeZone("GMT"); 973 } 974 else 975 { 976 timeZone = TimeZone.getTimeZone("GMT" + offset); 977 } 978 zone = "z"; 979 dateTime = dateTime.substring(0, dateTime.length()-6) + "GMT" + offset; 982 } 983 else 984 { 985 timeZone = TimeZone.getDefault(); 987 zone = ""; 988 } 991 String [] formats = {dt + zone, d, gym, gy}; 992 993 try 997 { 998 SimpleDateFormat inFormat = new SimpleDateFormat (t + zone); 999 inFormat.setLenient(false); 1000 Date d= inFormat.parse(dateTime); 1001 SimpleDateFormat outFormat = new SimpleDateFormat (strip 1002 (yearSymbols + monthSymbols + daySymbols, pattern)); 1003 outFormat.setTimeZone(timeZone); 1004 return outFormat.format(d); 1005 } 1006 catch (ParseException pe) 1007 { 1008 } 1009 1010 for (int i = 0; i < formats.length; i++) 1012 { 1013 try 1014 { 1015 SimpleDateFormat inFormat = new SimpleDateFormat (formats[i]); 1016 inFormat.setLenient(false); 1017 Date d = inFormat.parse(dateTime); 1018 SimpleDateFormat outFormat = new SimpleDateFormat (pattern); 1019 outFormat.setTimeZone(timeZone); 1020 return outFormat.format(d); 1021 } 1022 catch (ParseException pe) 1023 { 1024 } 1025 } 1026 1027 try 1032 { 1033 SimpleDateFormat inFormat = new SimpleDateFormat (gmd); 1034 inFormat.setLenient(false); 1035 Date d = inFormat.parse(dateTime); 1036 SimpleDateFormat outFormat = new SimpleDateFormat (strip(yearSymbols, pattern)); 1037 outFormat.setTimeZone(timeZone); 1038 return outFormat.format(d); 1039 } 1040 catch (ParseException pe) 1041 { 1042 } 1043 try 1044 { 1045 SimpleDateFormat inFormat = new SimpleDateFormat (gm); 1046 inFormat.setLenient(false); 1047 Date d = inFormat.parse(dateTime); 1048 SimpleDateFormat outFormat = new SimpleDateFormat (strip(yearSymbols, pattern)); 1049 outFormat.setTimeZone(timeZone); 1050 return outFormat.format(d); 1051 } 1052 catch (ParseException pe) 1053 { 1054 } 1055 try 1056 { 1057 SimpleDateFormat inFormat = new SimpleDateFormat (gd); 1058 inFormat.setLenient(false); 1059 Date d = inFormat.parse(dateTime); 1060 SimpleDateFormat outFormat = new SimpleDateFormat (strip(yearSymbols + monthSymbols, pattern)); 1061 outFormat.setTimeZone(timeZone); 1062 return outFormat.format(d); 1063 } 1064 catch (ParseException pe) 1065 { 1066 } 1067 return EMPTY_STR; 1068 } 1069 1070 1076 private static String strip(String symbols, String pattern) 1077 { 1078 int quoteSemaphore = 0; 1079 int i = 0; 1080 StringBuffer result = new StringBuffer (pattern.length()); 1081 1082 while (i < pattern.length()) 1083 { 1084 char ch = pattern.charAt(i); 1085 if (ch == '\'') 1086 { 1087 int endQuote = pattern.indexOf('\'', i + 1); 1090 if (endQuote == -1) 1091 { 1092 endQuote = pattern.length(); 1093 } 1094 result.append(pattern.substring(i, endQuote)); 1095 i = endQuote++; 1096 } 1097 else if (symbols.indexOf(ch) > -1) 1098 { 1099 i++; 1101 } 1102 else 1103 { 1104 result.append(ch); 1105 i++; 1106 } 1107 } 1108 return result.toString(); 1109 } 1110 1111} 1112 | Popular Tags |