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.

DateAndTime.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.hwpf.usermodel;
  16. import java.util.Calendar;
  17. import org.apache.poi.common.Duplicatable;
  18. import org.apache.poi.util.BitField;
  19. import org.apache.poi.util.BitFieldFactory;
  20. import org.apache.poi.util.LittleEndian;
  21. import org.apache.poi.util.LittleEndianConsts;
  22. import org.apache.poi.util.LocaleUtil;
  23. import org.apache.poi.util.Removal;
  24. /**
  25. * This class is used to represent a date and time in a Word document.
  26. */
  27. public final class DateAndTime implements Duplicatable {
  28. public static final int SIZE = 4;
  29. private static final BitField _minutes = BitFieldFactory.getInstance(0x3f);
  30. private static final BitField _hours = BitFieldFactory.getInstance(0x7c0);
  31. private static final BitField _dom = BitFieldFactory.getInstance(0xf800);
  32. private static final BitField _months = BitFieldFactory.getInstance(0xf);
  33. private static final BitField _years = BitFieldFactory.getInstance(0x1ff0);
  34. // private static final BitField _weekday = BitFieldFactory.getInstance(0xe000);
  35. private short _info;
  36. private short _info2;
  37. public DateAndTime() {}
  38. public DateAndTime(DateAndTime other) {
  39. _info = other._info;
  40. _info2 = other._info2;
  41. }
  42. public DateAndTime(byte[] buf, int offset) {
  43. _info = LittleEndian.getShort(buf, offset);
  44. _info2 = LittleEndian.getShort(buf, offset + LittleEndianConsts.SHORT_SIZE);
  45. }
  46. public Calendar getDate() {
  47. // TODO Discover if the timezone is stored somewhere else or not
  48. return LocaleUtil.getLocaleCalendar(
  49. _years.getValue(_info2)+1900,
  50. _months.getValue(_info2)-1,
  51. _dom.getValue(_info),
  52. _hours.getValue(_info),
  53. _minutes.getValue(_info),
  54. 0
  55. );
  56. }
  57. public void serialize(byte[] buf, int offset)
  58. {
  59. LittleEndian.putShort(buf, offset, _info);
  60. LittleEndian.putShort(buf, offset + LittleEndianConsts.SHORT_SIZE, _info2);
  61. }
  62. @Override
  63. public boolean equals(Object o)
  64. {
  65. if (!(o instanceof DateAndTime)) return false;
  66. DateAndTime dttm = (DateAndTime)o;
  67. return _info == dttm._info && _info2 == dttm._info2;
  68. }
  69. @Override
  70. public int hashCode() {
  71. assert false : "hashCode not designed";
  72. return 42; // any arbitrary constant will do
  73. }
  74. @Override
  75. @SuppressWarnings("squid:S2975")
  76. @Deprecated
  77. @Removal(version = "5.0.0")
  78. public DateAndTime clone() {
  79. return copy();
  80. }
  81. @Override
  82. public DateAndTime copy() {
  83. return new DateAndTime(this);
  84. }
  85. public boolean isEmpty()
  86. {
  87. return _info == 0 && _info2 == 0;
  88. }
  89. @Override
  90. public String toString()
  91. {
  92. if ( isEmpty() )
  93. return "[DTTM] EMPTY";
  94. return "[DTTM] " + getDate();
  95. }
  96. }