2
0

DateRange.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.script;
  16. import java.text.DateFormat;
  17. import java.text.ParseException;
  18. import java.util.Date;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. /**
  22. * @author Luis Arias
  23. */
  24. public class DateRange
  25. {
  26. protected static final Logger _log = Logger.getLogger(DateRange.class.getName());
  27. private final Date _startDate, _endDate;
  28. public DateRange(Date from, Date to)
  29. {
  30. _startDate = from;
  31. _endDate = to;
  32. }
  33. public static DateRange parse(String dateRange, DateFormat format)
  34. {
  35. String[] date = dateRange.split("-");
  36. if (date.length == 2)
  37. {
  38. try
  39. {
  40. Date start = format.parse(date[0]);
  41. Date end = format.parse(date[1]);
  42. return new DateRange(start, end);
  43. }
  44. catch (ParseException e)
  45. {
  46. _log.log(Level.WARNING, "Invalid Date Format.", e);
  47. }
  48. }
  49. return new DateRange(null, null);
  50. }
  51. public boolean isValid()
  52. {
  53. return (_startDate != null) && (_endDate != null) && _startDate.before(_endDate);
  54. }
  55. public boolean isWithinRange(Date date)
  56. {
  57. return date.after(_startDate) && date.before(_endDate);
  58. }
  59. public Date getEndDate()
  60. {
  61. return _endDate;
  62. }
  63. public Date getStartDate()
  64. {
  65. return _startDate;
  66. }
  67. @Override
  68. public String toString()
  69. {
  70. return "DateRange: From: " + getStartDate() + " To: " + getEndDate();
  71. }
  72. }