Skip to content

Commit 20f602d

Browse files
committed
Replaced SimpleDateFormat in the classes related to time series charts,
implemented static date/time parsing methods in the DateTimeHelper class
1 parent 7f42ab3 commit 20f602d

File tree

6 files changed

+80
-94
lines changed

6 files changed

+80
-94
lines changed

eclipseProjects/org.agentgui/bundles/de.enflexit.common/src/de/enflexit/common/DateTimeHelper.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,33 @@ public static String getDateTimeAsString(long utcTimeStamp, String pattern, Zone
228228
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(utcTimeStamp), zoneId).format(DateTimeFormatter.ofPattern(pattern));
229229
}
230230

231+
// ------------------------------------------------------------------------
232+
// --- From here, methods for parsing DateTime Strings --------------------
233+
// ------------------------------------------------------------------------
234+
235+
/**
236+
* Parses a date/time string, according to the specified format and zone. Returns a {@link ZonedDateTime} instance.
237+
* @param dateString the date string
238+
* @param dateFormat the date format
239+
* @param zoneId the zone id
240+
* @return the parsed ZonedDateTime instance
241+
*/
242+
public static ZonedDateTime getZonedDateTimeFromString(String dateString, String dateFormat, ZoneId zoneId) {
243+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat).withZone(zoneId);
244+
return ZonedDateTime.parse(dateString, dtf);
245+
}
246+
247+
/**
248+
* Parses a date/time string, according to the specified format and zone. Returns a long timestamp (epoch millis)
249+
* @param dateString the date string
250+
* @param dateFormat the date format
251+
* @param zoneId the zone id
252+
* @return the timestamp
253+
*/
254+
public static long getTimestampFromDateTimeString(String dateString, String dateFormat, ZoneId zoneId) {
255+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat).withZone(zoneId);
256+
ZonedDateTime zdt = ZonedDateTime.parse(dateString, dtf);
257+
return zdt.toInstant().toEpochMilli();
258+
}
259+
231260
}

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/core/charts/TableModel.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@
3030

3131
import jade.util.leap.List;
3232

33-
import java.text.DateFormat;
34-
import java.text.SimpleDateFormat;
3533
import java.util.Arrays;
3634
import java.util.Collection;
3735
import java.util.Collections;
38-
import java.util.Date;
3936
import java.util.HashSet;
4037
import java.util.Vector;
4138

@@ -45,8 +42,10 @@
4542
import javax.swing.table.TableColumn;
4643
import javax.swing.table.TableColumnModel;
4744

45+
import agentgui.core.config.GlobalInfo;
4846
import agentgui.ontology.DataSeries;
4947
import agentgui.ontology.ValuePair;
48+
import de.enflexit.common.DateTimeHelper;
5049

5150
/**
5251
* Abstract superclass for the table representation of chart data.
@@ -58,6 +57,8 @@ public abstract class TableModel extends AbstractTableModel implements TableMode
5857

5958
private static final long serialVersionUID = -6719770378777635821L;
6059

60+
private static final String DEFAULT_DATE_FORMAT = "dd.MM.yyyy HH:mm:ss";
61+
6162
protected JTable myJTable;
6263

6364
/** The column titles */
@@ -69,7 +70,6 @@ public abstract class TableModel extends AbstractTableModel implements TableMode
6970

7071
/** The parent ChartDataModel this ChartTableModel is part of. */
7172
protected DataModel parentDataModel;
72-
7373

7474
/**
7575
* Initialises the local table model and (re)sets the local values
@@ -368,8 +368,6 @@ public Vector<Vector<Object>> getTableDataAsObjectVector(boolean addHeaders){
368368
public Vector<Vector<Object>> getTableDataAsObjectVector(boolean addHeaders, boolean isTimeSeriesData){
369369
Vector<Vector<Object>> dataVector = new Vector<Vector<Object>>();
370370

371-
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
372-
373371
if(addHeaders == true){
374372
dataVector.addElement(new Vector<Object>(Arrays.asList(columnTitles.toArray())));
375373
}
@@ -379,9 +377,10 @@ public Vector<Vector<Object>> getTableDataAsObjectVector(boolean addHeaders, boo
379377
for(Vector<Number> numberVector : treeMapData){
380378
Vector<Object> objectVector = new Vector<Object>(Arrays.asList(numberVector.toArray()));
381379

380+
// --- For time series data, replace the timestamp with the corresponding date/time String
382381
if(isTimeSeriesData == true){
383382
long timestamp = (long) objectVector.get(0);
384-
String dateTime = dateFormat.format(new Date(timestamp));
383+
String dateTime = DateTimeHelper.getDateTimeAsString(timestamp, DEFAULT_DATE_FORMAT, GlobalInfo.getCurrentZoneId());
385384
objectVector.remove(0);
386385
objectVector.insertElementAt(dateTime, 0);
387386
}
@@ -402,16 +401,6 @@ protected Vector<Integer> getColumnWidths(){
402401
}
403402

404403
TableColumnModel tcm = this.myJTable.getColumnModel();
405-
406-
// Array based old approach
407-
// int[] columnWidths = new int[this.getColumnCount()];
408-
// for(int i=0; i<this.getColumnCount(); i++){
409-
// TableColumn tc = tcm.getColumn(i);
410-
// if(tc != null){
411-
// columnWidths[i] = tc.getWidth();
412-
// }
413-
// }
414-
415404
Vector<Integer> cwVector = new Vector<Integer>();
416405
for(TableColumn tc : Collections.list(tcm.getColumns())){
417406
cwVector.addElement(tc.getWidth());
@@ -433,4 +422,5 @@ protected void setColumnWidths(Vector<Integer> columnWidths){
433422
}
434423
}
435424
}
425+
436426
}

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/core/charts/gui/TableCellRenderer4Time.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,20 @@
2828
*/
2929
package agentgui.core.charts.gui;
3030

31-
import java.text.DateFormat;
32-
import java.text.SimpleDateFormat;
33-
import java.util.Date;
34-
31+
import java.time.Instant;
3532
import javax.swing.table.DefaultTableCellRenderer;
3633

34+
import agentgui.core.config.GlobalInfo;
35+
import de.enflexit.common.DateTimeHelper;
36+
3737
/**
38-
* Table cell renderer for dates. Displays the date as formatted String according to the system locale.
39-
* @author Nils
40-
*
38+
* Table cell renderer for date/time data. Displays the date/time as formatted String according the configured time format and zone.
39+
* @author Nils Loose - SOFTEC - Paluno - University of Duisburg-Essen
4140
*/
4241
public class TableCellRenderer4Time extends DefaultTableCellRenderer {
4342

4443
private String timeFormat;
45-
46-
/**
47-
*
48-
*/
44+
4945
private static final long serialVersionUID = 7378047653825108279L;
5046

5147
public TableCellRenderer4Time(String timeFormat){
@@ -57,19 +53,14 @@ public TableCellRenderer4Time(String timeFormat){
5753
*/
5854
@Override
5955
protected void setValue(Object value) {
60-
Date date = null;
61-
if (value==null) {
62-
date = new Date(0);
56+
Instant instant = null;
57+
if (value!=null && value instanceof Number) {
58+
long timeMillis = ((Number)value).longValue();
59+
instant = Instant.ofEpochMilli(timeMillis);
6360
} else {
64-
if (value instanceof Number) {
65-
Long lngValue = ((Number) value).longValue();
66-
date = new Date(lngValue);
67-
} else {
68-
date = new Date(0);
69-
}
61+
instant = Instant.now();
7062
}
71-
DateFormat timeFormat = new SimpleDateFormat(this.timeFormat);
72-
setText(timeFormat.format(date));
63+
this.setText(DateTimeHelper.getDateTimeAsString(instant.toEpochMilli(), this.timeFormat, GlobalInfo.getCurrentZoneId()));
7364
}
7465

7566
/**
@@ -78,5 +69,5 @@ protected void setValue(Object value) {
7869
public void setTimeFormat(String timeFormat) {
7970
this.timeFormat = timeFormat;
8071
}
81-
72+
8273
}

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/core/charts/timeseriesChart/gui/TimeFormatImportConfiguration.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@
4747
import java.io.FileNotFoundException;
4848
import java.io.FileReader;
4949
import java.io.IOException;
50-
import java.text.DateFormat;
51-
import java.text.ParseException;
52-
import java.text.SimpleDateFormat;
50+
import java.time.format.DateTimeParseException;
5351
import java.util.ArrayList;
5452
import java.util.Calendar;
5553
import java.util.Date;
@@ -79,6 +77,7 @@
7977
import agentgui.simulationService.time.TimeFormatSelection;
8078
import agentgui.simulationService.time.TimeModel;
8179
import agentgui.simulationService.time.TimeModelDateBased;
80+
import de.enflexit.common.DateTimeHelper;
8281
import de.enflexit.common.ExceptionHandling;
8382

8483
/**
@@ -139,8 +138,7 @@ public class TimeFormatImportConfiguration extends JDialog implements ActionList
139138
private JButton jButtonOK = null;
140139
private JButton jButtonCancel = null;
141140
private JLabel jLabelDummy = null;
142-
143-
141+
144142
/**
145143
* Instantiates a new time format import configuration.
146144
*
@@ -352,18 +350,15 @@ private void setExampleParse() {
352350
} else {
353351
try {
354352
// --- Try to parse the example String first --------
355-
DateFormat df = new SimpleDateFormat(this.getTimeFormat());
356-
Date dateParsed = df.parse(this.csvFileFirstTimeStamp);
357-
this.timeExampleFromFile = dateParsed.getTime();
353+
this.timeExampleFromFile = DateTimeHelper.getTimestampFromDateTimeString(this.csvFileFirstTimeStamp, this.getTimeFormat(), GlobalInfo.getCurrentZoneId());
358354

359355
// --- Try to display the date in a standard way ----
360-
DateFormat dfParsed = new SimpleDateFormat(TimeModelDateBased.DEFAULT_TIME_FORMAT);
361-
this.getJTextFieldParsed().setText(dfParsed.format(dateParsed).toString());
356+
this.getJTextFieldParsed().setText(DateTimeHelper.getDateTimeAsString(this.timeExampleFromFile, TimeModelDateBased.DEFAULT_TIME_FORMAT, GlobalInfo.getCurrentZoneId()));
362357
this.setError(false);
363358
this.getJTextFieldParsed().setForeground(new Color(0, 0, 0));
364359
this.jLabelParsed.setForeground(new Color(0, 0, 0));
365360

366-
} catch (ParseException pe) {
361+
} catch (DateTimeParseException pe) {
367362
// --- Error while parsing --------------------------
368363
String exceptionShort = ExceptionHandling.getFirstTextLineOfException(pe);
369364
if (exceptionShort.contains(":")==true) {
@@ -571,8 +566,9 @@ private JLabel getJLabelSimulationStartTime() {
571566
if (this.timeStartSimSetup==null) {
572567
displayText += " [" + Language.translate("Not defined!", Language.EN) + "]";
573568
} else {
574-
displayText += " " + new SimpleDateFormat(TimeModelDateBased.DEFAULT_TIME_FORMAT).format(new Date(this.timeStartSimSetup));
575-
displayText += " - <b>" + Language.translate("Formatted as", Language.EN) + ":</b> " + new SimpleDateFormat(timeFormatSimSetup).format(new Date(this.timeStartSimSetup)) + "";
569+
// displayText += " " + new SimpleDateFormat(TimeModelDateBased.DEFAULT_TIME_FORMAT).format(new Date(this.timeStartSimSetup));
570+
displayText += " " + DateTimeHelper.getDateTimeAsString(this.timeStartSimSetup, TimeModelDateBased.DEFAULT_TIME_FORMAT, GlobalInfo.getCurrentZoneId());
571+
displayText += " - <b>" + Language.translate("Formatted as", Language.EN) + ":</b> " + DateTimeHelper.getDateTimeAsString(this.timeStartSimSetup, this.timeFormatSimSetup, GlobalInfo.getCurrentZoneId());
576572
}
577573
displayText += "</html>";
578574

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/core/charts/timeseriesChart/gui/TimeSeriesChartEditorJPanel.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@
2828
*/
2929
package agentgui.core.charts.timeseriesChart.gui;
3030

31-
import java.text.DateFormat;
32-
import java.text.ParseException;
33-
import java.text.SimpleDateFormat;
34-
import java.util.Date;
35-
31+
import java.time.ZonedDateTime;
32+
import java.time.format.DateTimeFormatter;
3633
import javax.swing.JToolBar;
3734

3835
import agentgui.core.charts.gui.ChartEditorJPanel;
3936
import agentgui.core.charts.timeseriesChart.TimeSeriesDataModel;
4037
import agentgui.core.charts.timeseriesChart.TimeSeriesOntologyModel;
38+
import agentgui.core.config.GlobalInfo;
4139
import agentgui.ontology.Chart;
4240
import de.enflexit.common.ontology.gui.DynForm;
4341

@@ -134,19 +132,14 @@ protected TimeSeriesChartSettingsTab getChartSettingsTab(){
134132
@Override
135133
protected Number parseKey(String key, String keyFormat, Number keyOffset) {
136134

137-
Long timestamp = (long) 0;
138-
try {
139-
DateFormat df = new SimpleDateFormat(keyFormat);
140-
Date dateParsed = df.parse(key);
141-
timestamp = dateParsed.getTime();
142-
if (keyOffset!=null) {
143-
timestamp = timestamp + (Long) keyOffset;
144-
}
145-
// System.out.println( "=> " + new Date(timestamp) );
135+
long timestamp = 0;
136+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(keyFormat).withZone(GlobalInfo.getCurrentZoneId());
137+
ZonedDateTime zdt = ZonedDateTime.parse(key, formatter);
138+
timestamp = zdt.toInstant().toEpochMilli();
139+
if (keyOffset!=null) {
140+
timestamp = timestamp + (Long) keyOffset;
141+
}
146142

147-
} catch (ParseException e) {
148-
e.printStackTrace();
149-
}
150143
return timestamp;
151144
}
152145

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/core/charts/timeseriesChart/gui/TimeSeriesChartTab.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,24 @@
2828
*/
2929
package agentgui.core.charts.timeseriesChart.gui;
3030

31-
import java.text.DateFormat;
32-
import java.text.SimpleDateFormat;
33-
3431
import org.jfree.chart.ChartFactory;
3532
import org.jfree.chart.axis.DateAxis;
3633
import org.jfree.data.time.TimeSeriesCollection;
3734

3835
import agentgui.core.charts.DataModel;
3936
import agentgui.core.charts.gui.ChartTab;
4037
import agentgui.core.charts.timeseriesChart.TimeSeriesDataModel;
38+
import agentgui.core.config.GlobalInfo;
39+
import de.enflexit.common.swing.TimeZoneDateFormat;
4140

4241
/**
4342
* Time series chart visualization component
44-
* @author Nils
45-
*
43+
* @author Nils Loose - SOFTEC - Paluno - University of Duisburg-Essen
4644
*/
4745
public class TimeSeriesChartTab extends ChartTab {
4846

4947
private static final long serialVersionUID = -1998969136744482400L;
5048

51-
5249
/**
5350
* Instantiates a new time series chart tab.
5451
*
@@ -66,29 +63,19 @@ public TimeSeriesChartTab(TimeSeriesDataModel model, TimeSeriesChartEditorJPanel
6663
), parent);
6764

6865
this.dataModel = model;
69-
7066
this.applySettings();
71-
7267
model.getChartModel().addObserver(this);
7368
}
7469

70+
/* (non-Javadoc)
71+
* @see agentgui.core.charts.gui.ChartTab#replaceModel(agentgui.core.charts.DataModel)
72+
*/
7573
@Override
7674
public void replaceModel(DataModel newModel) {
7775
this.dataModel = newModel;
7876

79-
// this.chartPanel.setChart(ChartFactory.createTimeSeriesChart(
80-
// this.dataModel.getOntologyModel().getChartSettings().getChartTitle(),
81-
// this.dataModel.getOntologyModel().getChartSettings().getXAxisLabel(),
82-
// this.dataModel.getOntologyModel().getChartSettings().getYAxisLabel(),
83-
// ((TimeSeriesDataModel)this.dataModel).getTimeSeriesChartModel().getTimeSeriesCollection(),
84-
// true, false, false
85-
// ));
86-
//
87-
// applySettings();
88-
8977
TimeSeriesCollection tsc = ((TimeSeriesDataModel)this.dataModel).getTimeSeriesChartModel().getTimeSeriesCollection();
9078
this.chartPanel.getChart().getXYPlot().setDataset(tsc);
91-
9279
dataModel.getChartModel().addObserver(this);
9380
}
9481

@@ -99,19 +86,19 @@ public void replaceModel(DataModel newModel) {
9986
protected void applySettings() {
10087
super.applySettings();
10188
DateAxis da = (DateAxis) this.chartPanel.getChart().getXYPlot().getDomainAxis();
102-
103-
DateFormat dateFormat = new SimpleDateFormat(((TimeSeriesDataModel)dataModel).getTimeFormat());
104-
da.setDateFormatOverride(dateFormat);
89+
String formatString = ((TimeSeriesDataModel)dataModel).getTimeFormat();
90+
TimeZoneDateFormat tzdf = new TimeZoneDateFormat(formatString, GlobalInfo.getCurrentZoneId());
91+
da.setDateFormatOverride(tzdf);
10592
}
10693

10794
/**
10895
* Sets the time format for the time axis label ticks
10996
* @param timeFormat
11097
*/
11198
void setTimeFormat(String timeFormat){
112-
DateFormat dateFormat = new SimpleDateFormat(timeFormat);
99+
TimeZoneDateFormat tzdf = new TimeZoneDateFormat(timeFormat, GlobalInfo.getCurrentZoneId());
113100
DateAxis da = (DateAxis) this.chartPanel.getChart().getXYPlot().getDomainAxis();
114-
da.setDateFormatOverride(dateFormat);
101+
da.setDateFormatOverride(tzdf);
115102
}
116103

117104
}

0 commit comments

Comments
 (0)