DateRange.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.after(_startDate) && date.before(_endDate);
  62. }
  63. public Date getEndDate()
  64. {
  65. return _endDate;
  66. }
  67. public Date getStartDate()
  68. {
  69. return _startDate;
  70. }
  71. @Override
  72. public String toString()
  73. {
  74. return "DateRange: From: " + getStartDate() + " To: " + getEndDate();
  75. }
  76. }