2
0

DateRange.java 2.0 KB

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