DateRange.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. */
  25. public class DateRange
  26. {
  27. protected static final Logger _log = Logger.getLogger(DateRange.class.getName());
  28. private Date _startDate, _endDate;
  29. public DateRange(Date from, Date to)
  30. {
  31. _startDate = from;
  32. _endDate = to;
  33. }
  34. public static DateRange parse(String dateRange, DateFormat format)
  35. {
  36. String[] date = dateRange.split("-");
  37. if (date.length == 2)
  38. {
  39. try
  40. {
  41. Date start = format.parse(date[0]);
  42. Date end = format.parse(date[1]);
  43. return new DateRange(start, end);
  44. }
  45. catch (ParseException e)
  46. {
  47. _log.log(Level.WARNING, "Invalid Date Format.", e);
  48. }
  49. }
  50. return new DateRange(null, null);
  51. }
  52. public boolean isValid()
  53. {
  54. return _startDate == null || _endDate == null;
  55. }
  56. public boolean isWithinRange(Date date)
  57. {
  58. return date.after(_startDate) && date.before(_endDate);
  59. }
  60. public Date getEndDate()
  61. {
  62. return _endDate;
  63. }
  64. public Date getStartDate()
  65. {
  66. return _startDate;
  67. }
  68. }