DateRange.java 2.2 KB

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