001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2022, by David Gilbert and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * -------------- 028 * DateRange.java 029 * -------------- 030 * (C) Copyright 2002-2022, by David Gilbert and Contributors. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): Bill Kelemen; 034 * 035 */ 036 037package org.jfree.data.time; 038 039import java.io.Serializable; 040import java.text.DateFormat; 041import java.util.Date; 042 043import org.jfree.data.Range; 044 045/** 046 * A range specified in terms of two {@code java.util.Date} objects. 047 * Instances of this class are immutable. 048 */ 049public class DateRange extends Range implements Serializable { 050 051 /** For serialization. */ 052 private static final long serialVersionUID = -4705682568375418157L; 053 054 /** The lower bound for the range. */ 055 private final long lowerDate; 056 057 /** The upper bound for the range. */ 058 private final long upperDate; 059 060 /** 061 * Default constructor. 062 */ 063 public DateRange() { 064 this(new Date(0), new Date(1)); 065 } 066 067 /** 068 * Constructs a new range. 069 * 070 * @param lower the lower bound ({@code null} not permitted). 071 * @param upper the upper bound ({@code null} not permitted). 072 */ 073 public DateRange(Date lower, Date upper) { 074 super(lower.getTime(), upper.getTime()); 075 this.lowerDate = lower.getTime(); 076 this.upperDate = upper.getTime(); 077 } 078 079 /** 080 * Constructs a new range using two values that will be interpreted as 081 * "milliseconds since midnight GMT, 1-Jan-1970". 082 * 083 * @param lower the lower (oldest) date. 084 * @param upper the upper (most recent) date. 085 */ 086 public DateRange(double lower, double upper) { 087 super(lower, upper); 088 this.lowerDate = (long) lower; 089 this.upperDate = (long) upper; 090 } 091 092 /** 093 * Constructs a new range that is based on another {@link Range}. The 094 * other range does not have to be a {@link DateRange}. If it is not, the 095 * upper and lower bounds are evaluated as milliseconds since midnight 096 * GMT, 1-Jan-1970. 097 * 098 * @param other the other range ({@code null} not permitted). 099 */ 100 public DateRange(Range other) { 101 this(other.getLowerBound(), other.getUpperBound()); 102 } 103 104 /** 105 * Returns the lower (earlier) date for the range. 106 * 107 * @return The lower date for the range. 108 * 109 * @see #getLowerMillis() 110 */ 111 public Date getLowerDate() { 112 return new Date(this.lowerDate); 113 } 114 115 /** 116 * Returns the lower bound of the range in milliseconds. 117 * 118 * @return The lower bound. 119 * 120 * @see #getLowerDate() 121 * 122 * @since 1.0.11 123 */ 124 public long getLowerMillis() { 125 return this.lowerDate; 126 } 127 128 /** 129 * Returns the upper (later) date for the range. 130 * 131 * @return The upper date for the range. 132 * 133 * @see #getUpperMillis() 134 */ 135 public Date getUpperDate() { 136 return new Date(this.upperDate); 137 } 138 139 /** 140 * Returns the upper bound of the range in milliseconds. 141 * 142 * @return The upper bound. 143 * 144 * @see #getUpperDate() 145 * 146 * @since 1.0.11 147 */ 148 public long getUpperMillis() { 149 return this.upperDate; 150 } 151 152 /** 153 * Returns a string representing the date range (useful for debugging). 154 * 155 * @return A string representing the date range. 156 */ 157 @Override 158 public String toString() { 159 DateFormat df = DateFormat.getDateTimeInstance(); 160 return "[" + df.format(getLowerDate()) + " --> " 161 + df.format(getUpperDate()) + "]"; 162 } 163 164}