You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExcelStyleDateFormatter.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.usermodel;
  16. import java.math.RoundingMode;
  17. import java.text.DateFormatSymbols;
  18. import java.text.DecimalFormat;
  19. import java.text.DecimalFormatSymbols;
  20. import java.text.FieldPosition;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.Locale;
  24. import org.apache.poi.util.LocaleUtil;
  25. /**
  26. * A wrapper around a {@link SimpleDateFormat} instance,
  27. * which handles a few Excel-style extensions that
  28. * are not supported by {@link SimpleDateFormat}.
  29. * Currently, the extensions are around the handling
  30. * of elapsed time, eg rendering 1 day 2 hours
  31. * as 26 hours.
  32. */
  33. public class ExcelStyleDateFormatter extends SimpleDateFormat {
  34. public static final char MMMMM_START_SYMBOL = '\ue001';
  35. public static final char MMMMM_TRUNCATE_SYMBOL = '\ue002';
  36. public static final char H_BRACKET_SYMBOL = '\ue010';
  37. public static final char HH_BRACKET_SYMBOL = '\ue011';
  38. public static final char M_BRACKET_SYMBOL = '\ue012';
  39. public static final char MM_BRACKET_SYMBOL = '\ue013';
  40. public static final char S_BRACKET_SYMBOL = '\ue014';
  41. public static final char SS_BRACKET_SYMBOL = '\ue015';
  42. public static final char L_BRACKET_SYMBOL = '\ue016';
  43. public static final char LL_BRACKET_SYMBOL = '\ue017';
  44. private static final DecimalFormat format1digit;
  45. private static final DecimalFormat format2digits;
  46. private static final DecimalFormat format3digit;
  47. private static final DecimalFormat format4digits;
  48. static {
  49. DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(Locale.ROOT);
  50. format1digit = new DecimalFormat("0", dfs);
  51. format2digits = new DecimalFormat("00", dfs);
  52. format3digit = new DecimalFormat("0", dfs);
  53. format4digits = new DecimalFormat("00", dfs);
  54. DataFormatter.setExcelStyleRoundingMode(format1digit, RoundingMode.DOWN);
  55. DataFormatter.setExcelStyleRoundingMode(format2digits, RoundingMode.DOWN);
  56. DataFormatter.setExcelStyleRoundingMode(format3digit);
  57. DataFormatter.setExcelStyleRoundingMode(format4digits);
  58. }
  59. {
  60. setTimeZone(LocaleUtil.getUserTimeZone());
  61. }
  62. private double dateToBeFormatted;
  63. // no-arg constructor is private because of undefined super call with locale
  64. public ExcelStyleDateFormatter(String pattern) {
  65. super(processFormatPattern(pattern), LocaleUtil.getUserLocale());
  66. }
  67. public ExcelStyleDateFormatter(String pattern,
  68. DateFormatSymbols formatSymbols) {
  69. super(processFormatPattern(pattern), formatSymbols);
  70. }
  71. public ExcelStyleDateFormatter(String pattern, Locale locale) {
  72. super(processFormatPattern(pattern), locale);
  73. }
  74. /**
  75. * Takes a format String, and replaces Excel specific bits
  76. * with our detection sequences
  77. */
  78. private static String processFormatPattern(String f) {
  79. String t = f.replace("MMMMM", MMMMM_START_SYMBOL + "MMM" + MMMMM_TRUNCATE_SYMBOL);
  80. t = t.replace("[H]", String.valueOf(H_BRACKET_SYMBOL));
  81. t = t.replace("[HH]", String.valueOf(HH_BRACKET_SYMBOL));
  82. t = t.replace("[m]", String.valueOf(M_BRACKET_SYMBOL));
  83. t = t.replace("[mm]", String.valueOf(MM_BRACKET_SYMBOL));
  84. t = t.replace("[s]", String.valueOf(S_BRACKET_SYMBOL));
  85. t = t.replace("[ss]", String.valueOf(SS_BRACKET_SYMBOL));
  86. t = t.replaceAll("s.000", "s.SSS");
  87. t = t.replaceAll("s.00", "s." + LL_BRACKET_SYMBOL);
  88. t = t.replaceAll("s.0", "s." + L_BRACKET_SYMBOL);
  89. return t;
  90. }
  91. /**
  92. * Used to let us know what the date being
  93. * formatted is, in Excel terms, which we
  94. * may wish to use when handling elapsed
  95. * times.
  96. */
  97. public void setDateToBeFormatted(double date) {
  98. this.dateToBeFormatted = date;
  99. }
  100. @Override
  101. public StringBuffer format(Date date, StringBuffer paramStringBuffer,
  102. FieldPosition paramFieldPosition) {
  103. // Do the normal format
  104. String s = super.format(date, paramStringBuffer, paramFieldPosition).toString();
  105. // Now handle our special cases
  106. if (s.indexOf(MMMMM_START_SYMBOL) != -1) {
  107. s = s.replaceAll(
  108. MMMMM_START_SYMBOL + "(\\p{L}|\\p{P})[\\p{L}\\p{P}]+" + MMMMM_TRUNCATE_SYMBOL,
  109. "$1"
  110. );
  111. }
  112. if (s.indexOf(H_BRACKET_SYMBOL) != -1 ||
  113. s.indexOf(HH_BRACKET_SYMBOL) != -1) {
  114. float hours = (float) dateToBeFormatted * 24;
  115. s = s.replaceAll(
  116. String.valueOf(H_BRACKET_SYMBOL),
  117. format1digit.format(hours)
  118. );
  119. s = s.replaceAll(
  120. String.valueOf(HH_BRACKET_SYMBOL),
  121. format2digits.format(hours)
  122. );
  123. }
  124. if (s.indexOf(M_BRACKET_SYMBOL) != -1 ||
  125. s.indexOf(MM_BRACKET_SYMBOL) != -1) {
  126. float minutes = (float) dateToBeFormatted * 24 * 60;
  127. s = s.replaceAll(
  128. String.valueOf(M_BRACKET_SYMBOL),
  129. format1digit.format(minutes)
  130. );
  131. s = s.replaceAll(
  132. String.valueOf(MM_BRACKET_SYMBOL),
  133. format2digits.format(minutes)
  134. );
  135. }
  136. if (s.indexOf(S_BRACKET_SYMBOL) != -1 ||
  137. s.indexOf(SS_BRACKET_SYMBOL) != -1) {
  138. float seconds = (float) (dateToBeFormatted * 24 * 60 * 60);
  139. s = s.replaceAll(
  140. String.valueOf(S_BRACKET_SYMBOL),
  141. format1digit.format(seconds)
  142. );
  143. s = s.replaceAll(
  144. String.valueOf(SS_BRACKET_SYMBOL),
  145. format2digits.format(seconds)
  146. );
  147. }
  148. if (s.indexOf(L_BRACKET_SYMBOL) != -1 ||
  149. s.indexOf(LL_BRACKET_SYMBOL) != -1) {
  150. float millisTemp = (float) ((dateToBeFormatted - Math.floor(dateToBeFormatted)) * 24 * 60 * 60);
  151. float millis = (millisTemp - (int) millisTemp);
  152. s = s.replaceAll(
  153. String.valueOf(L_BRACKET_SYMBOL),
  154. format3digit.format(millis * 10.0)
  155. );
  156. s = s.replaceAll(
  157. String.valueOf(LL_BRACKET_SYMBOL),
  158. format4digits.format(millis * 100.0)
  159. );
  160. }
  161. return new StringBuffer(s);
  162. }
  163. @Override
  164. public boolean equals(Object o) {
  165. if (!(o instanceof ExcelStyleDateFormatter)) {
  166. return false;
  167. }
  168. ExcelStyleDateFormatter other = (ExcelStyleDateFormatter) o;
  169. return dateToBeFormatted == other.dateToBeFormatted;
  170. }
  171. @Override
  172. public int hashCode() {
  173. return Double.valueOf(dateToBeFormatted).hashCode();
  174. }
  175. }