1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.gscg.singleuser.interactionlayer.dataobjects;
15
16 import java.io.Serializable;
17 import java.rmi.RemoteException;
18 import java.util.Calendar;
19
20 import nl.tudelft.simulation.dsol.experiment.TimeUnit;
21 import nl.tudelft.simulation.dsol.experiment.TimeUnitInterface;
22 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
23 import nl.tudelft.simulation.logger.Logger;
24
25 /***
26 * The DateIntData object takes care of parsing the data in the form of days,
27 * months and years into a DateFormat, or vice versa, taken into account the
28 * starttime of the simulator. It is used by a number of content related data
29 * objects in order to easily calculate the date as a string.
30 * <p>
31 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
32 * Delft, the Netherlands. All rights reserved.
33 *
34 * See for project information <a href="http://www.simulation.tudelft.nl/">
35 * www.simulation.tudelft.nl </a>.
36 *
37 * The source code and binary code of this software is proprietary information
38 * of Delft University of Technology.
39 *
40 * @author <a
41 * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
42 * Verbraeck </a>
43 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:34:00 $
44 * @since 1.0.0
45 */
46 public class DateIntData implements Serializable
47 {
48 /*** the serial version uid */
49 private static final long serialVersionUID = 11L;
50
51 /*** the year */
52 private int year;
53
54 /*** the month */
55 private int month;
56
57 /*** the day */
58 private int day;
59
60 /***
61 * @param dateIntData the date of a date in integers
62 * @param simulator the simulator
63 * @return returns a double which reflects the simulator time
64 */
65 public static double makeSimulationDate(final DateIntData dateIntData,
66 final SimulatorInterface simulator)
67 {
68 Calendar calendar = Calendar.getInstance();
69 calendar.set(dateIntData.getYear(), dateIntData.getMonth(), dateIntData
70 .getDay());
71 double simulatorTime = 0.0;
72 try
73 {
74 long time = calendar.getTimeInMillis()
75 - simulator.getReplication().getRunControl().getTreatment()
76 .getStartTime();
77
78 simulatorTime = TimeUnit.convert(time,
79 TimeUnitInterface.MILLISECOND, simulator);
80
81 } catch (RemoteException exception)
82 {
83 Logger.severe(DateIntData.class, "makeSimulationDate",
84 "could not convert date");
85 }
86 return simulatorTime;
87 }
88
89 /***
90 * @param date the date
91 * @param simulator the simulator
92 * @return returns a DateIntData object
93 */
94 public static DateIntData makeDateIntData(final double date,
95 final SimulatorInterface simulator)
96 {
97 Calendar calendar = Calendar.getInstance();
98 long time = 0L;
99 try
100 {
101 time = (long) TimeUnit.convert(date, simulator.getReplication()
102 .getRunControl().getTreatment().getTimeUnit(),
103 TimeUnitInterface.MILLISECOND);
104
105 calendar.setTimeInMillis((time)
106 + simulator.getReplication().getRunControl().getTreatment()
107 .getStartTime());
108
109 } catch (RemoteException exception)
110 {
111 Logger.severe(DateIntData.class, "makeSimulationDate",
112 "could not convert date");
113 }
114 return new DateIntData(calendar.get(Calendar.YEAR), calendar
115 .get(Calendar.MONTH), calendar.get(Calendar.DATE));
116 }
117
118 /***
119 * @param year the year
120 * @param month the month
121 * @param day the day
122 */
123 public DateIntData(final int year, final int month, final int day)
124 {
125 this.year = year;
126 this.month = month;
127 this.day = day;
128 }
129
130 /***
131 * @return Returns the day.
132 */
133 public int getDay()
134 {
135 return this.day;
136 }
137
138 /***
139 * @return Returns the month.
140 */
141 public int getMonth()
142 {
143 return this.month;
144 }
145
146 /***
147 * @return Returns the year.
148 */
149 public int getYear()
150 {
151 return this.year;
152 }
153
154 /***
155 * @see java.lang.Object#toString()
156 */
157 public String toString()
158 {
159
160
161 return this.day + "-" + (this.month + 1) + "-" + this.year;
162 }
163 }