Skip to content

Commit

Permalink
Replaced SimpleDateFormat in the classes related to time series charts,
Browse files Browse the repository at this point in the history
implemented static date/time parsing methods in the DateTimeHelper class
  • Loading branch information
NLoos committed Jan 7, 2022
1 parent 7f42ab3 commit 20f602d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,33 @@ public static String getDateTimeAsString(long utcTimeStamp, String pattern, Zone
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(utcTimeStamp), zoneId).format(DateTimeFormatter.ofPattern(pattern));
}

// ------------------------------------------------------------------------
// --- From here, methods for parsing DateTime Strings --------------------
// ------------------------------------------------------------------------

/**
* Parses a date/time string, according to the specified format and zone. Returns a {@link ZonedDateTime} instance.
* @param dateString the date string
* @param dateFormat the date format
* @param zoneId the zone id
* @return the parsed ZonedDateTime instance
*/
public static ZonedDateTime getZonedDateTimeFromString(String dateString, String dateFormat, ZoneId zoneId) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat).withZone(zoneId);
return ZonedDateTime.parse(dateString, dtf);
}

/**
* Parses a date/time string, according to the specified format and zone. Returns a long timestamp (epoch millis)
* @param dateString the date string
* @param dateFormat the date format
* @param zoneId the zone id
* @return the timestamp
*/
public static long getTimestampFromDateTimeString(String dateString, String dateFormat, ZoneId zoneId) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat).withZone(zoneId);
ZonedDateTime zdt = ZonedDateTime.parse(dateString, dtf);
return zdt.toInstant().toEpochMilli();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@

import jade.util.leap.List;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Vector;

Expand All @@ -45,8 +42,10 @@
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import agentgui.core.config.GlobalInfo;
import agentgui.ontology.DataSeries;
import agentgui.ontology.ValuePair;
import de.enflexit.common.DateTimeHelper;

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

private static final long serialVersionUID = -6719770378777635821L;

private static final String DEFAULT_DATE_FORMAT = "dd.MM.yyyy HH:mm:ss";

protected JTable myJTable;

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

/** The parent ChartDataModel this ChartTableModel is part of. */
protected DataModel parentDataModel;


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

DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

if(addHeaders == true){
dataVector.addElement(new Vector<Object>(Arrays.asList(columnTitles.toArray())));
}
Expand All @@ -379,9 +377,10 @@ public Vector<Vector<Object>> getTableDataAsObjectVector(boolean addHeaders, boo
for(Vector<Number> numberVector : treeMapData){
Vector<Object> objectVector = new Vector<Object>(Arrays.asList(numberVector.toArray()));

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

TableColumnModel tcm = this.myJTable.getColumnModel();

// Array based old approach
// int[] columnWidths = new int[this.getColumnCount()];
// for(int i=0; i<this.getColumnCount(); i++){
// TableColumn tc = tcm.getColumn(i);
// if(tc != null){
// columnWidths[i] = tc.getWidth();
// }
// }

Vector<Integer> cwVector = new Vector<Integer>();
for(TableColumn tc : Collections.list(tcm.getColumns())){
cwVector.addElement(tc.getWidth());
Expand All @@ -433,4 +422,5 @@ protected void setColumnWidths(Vector<Integer> columnWidths){
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,20 @@
*/
package agentgui.core.charts.gui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.time.Instant;
import javax.swing.table.DefaultTableCellRenderer;

import agentgui.core.config.GlobalInfo;
import de.enflexit.common.DateTimeHelper;

/**
* Table cell renderer for dates. Displays the date as formatted String according to the system locale.
* @author Nils
*
* Table cell renderer for date/time data. Displays the date/time as formatted String according the configured time format and zone.
* @author Nils Loose - SOFTEC - Paluno - University of Duisburg-Essen
*/
public class TableCellRenderer4Time extends DefaultTableCellRenderer {

private String timeFormat;

/**
*
*/

private static final long serialVersionUID = 7378047653825108279L;

public TableCellRenderer4Time(String timeFormat){
Expand All @@ -57,19 +53,14 @@ public TableCellRenderer4Time(String timeFormat){
*/
@Override
protected void setValue(Object value) {
Date date = null;
if (value==null) {
date = new Date(0);
Instant instant = null;
if (value!=null && value instanceof Number) {
long timeMillis = ((Number)value).longValue();
instant = Instant.ofEpochMilli(timeMillis);
} else {
if (value instanceof Number) {
Long lngValue = ((Number) value).longValue();
date = new Date(lngValue);
} else {
date = new Date(0);
}
instant = Instant.now();
}
DateFormat timeFormat = new SimpleDateFormat(this.timeFormat);
setText(timeFormat.format(date));
this.setText(DateTimeHelper.getDateTimeAsString(instant.toEpochMilli(), this.timeFormat, GlobalInfo.getCurrentZoneId()));
}

/**
Expand All @@ -78,5 +69,5 @@ protected void setValue(Object value) {
public void setTimeFormat(String timeFormat) {
this.timeFormat = timeFormat;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -79,6 +77,7 @@
import agentgui.simulationService.time.TimeFormatSelection;
import agentgui.simulationService.time.TimeModel;
import agentgui.simulationService.time.TimeModelDateBased;
import de.enflexit.common.DateTimeHelper;
import de.enflexit.common.ExceptionHandling;

/**
Expand Down Expand Up @@ -139,8 +138,7 @@ public class TimeFormatImportConfiguration extends JDialog implements ActionList
private JButton jButtonOK = null;
private JButton jButtonCancel = null;
private JLabel jLabelDummy = null;



/**
* Instantiates a new time format import configuration.
*
Expand Down Expand Up @@ -352,18 +350,15 @@ private void setExampleParse() {
} else {
try {
// --- Try to parse the example String first --------
DateFormat df = new SimpleDateFormat(this.getTimeFormat());
Date dateParsed = df.parse(this.csvFileFirstTimeStamp);
this.timeExampleFromFile = dateParsed.getTime();
this.timeExampleFromFile = DateTimeHelper.getTimestampFromDateTimeString(this.csvFileFirstTimeStamp, this.getTimeFormat(), GlobalInfo.getCurrentZoneId());

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

} catch (ParseException pe) {
} catch (DateTimeParseException pe) {
// --- Error while parsing --------------------------
String exceptionShort = ExceptionHandling.getFirstTextLineOfException(pe);
if (exceptionShort.contains(":")==true) {
Expand Down Expand Up @@ -571,8 +566,9 @@ private JLabel getJLabelSimulationStartTime() {
if (this.timeStartSimSetup==null) {
displayText += " [" + Language.translate("Not defined!", Language.EN) + "]";
} else {
displayText += " " + new SimpleDateFormat(TimeModelDateBased.DEFAULT_TIME_FORMAT).format(new Date(this.timeStartSimSetup));
displayText += " - <b>" + Language.translate("Formatted as", Language.EN) + ":</b> " + new SimpleDateFormat(timeFormatSimSetup).format(new Date(this.timeStartSimSetup)) + "";
// displayText += " " + new SimpleDateFormat(TimeModelDateBased.DEFAULT_TIME_FORMAT).format(new Date(this.timeStartSimSetup));
displayText += " " + DateTimeHelper.getDateTimeAsString(this.timeStartSimSetup, TimeModelDateBased.DEFAULT_TIME_FORMAT, GlobalInfo.getCurrentZoneId());
displayText += " - <b>" + Language.translate("Formatted as", Language.EN) + ":</b> " + DateTimeHelper.getDateTimeAsString(this.timeStartSimSetup, this.timeFormatSimSetup, GlobalInfo.getCurrentZoneId());
}
displayText += "</html>";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
*/
package agentgui.core.charts.timeseriesChart.gui;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import javax.swing.JToolBar;

import agentgui.core.charts.gui.ChartEditorJPanel;
import agentgui.core.charts.timeseriesChart.TimeSeriesDataModel;
import agentgui.core.charts.timeseriesChart.TimeSeriesOntologyModel;
import agentgui.core.config.GlobalInfo;
import agentgui.ontology.Chart;
import de.enflexit.common.ontology.gui.DynForm;

Expand Down Expand Up @@ -134,19 +132,14 @@ protected TimeSeriesChartSettingsTab getChartSettingsTab(){
@Override
protected Number parseKey(String key, String keyFormat, Number keyOffset) {

Long timestamp = (long) 0;
try {
DateFormat df = new SimpleDateFormat(keyFormat);
Date dateParsed = df.parse(key);
timestamp = dateParsed.getTime();
if (keyOffset!=null) {
timestamp = timestamp + (Long) keyOffset;
}
// System.out.println( "=> " + new Date(timestamp) );
long timestamp = 0;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(keyFormat).withZone(GlobalInfo.getCurrentZoneId());
ZonedDateTime zdt = ZonedDateTime.parse(key, formatter);
timestamp = zdt.toInstant().toEpochMilli();
if (keyOffset!=null) {
timestamp = timestamp + (Long) keyOffset;
}

} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,24 @@
*/
package agentgui.core.charts.timeseriesChart.gui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.axis.DateAxis;
import org.jfree.data.time.TimeSeriesCollection;

import agentgui.core.charts.DataModel;
import agentgui.core.charts.gui.ChartTab;
import agentgui.core.charts.timeseriesChart.TimeSeriesDataModel;
import agentgui.core.config.GlobalInfo;
import de.enflexit.common.swing.TimeZoneDateFormat;

/**
* Time series chart visualization component
* @author Nils
*
* @author Nils Loose - SOFTEC - Paluno - University of Duisburg-Essen
*/
public class TimeSeriesChartTab extends ChartTab {

private static final long serialVersionUID = -1998969136744482400L;


/**
* Instantiates a new time series chart tab.
*
Expand All @@ -66,29 +63,19 @@ public TimeSeriesChartTab(TimeSeriesDataModel model, TimeSeriesChartEditorJPanel
), parent);

this.dataModel = model;

this.applySettings();

model.getChartModel().addObserver(this);
}

/* (non-Javadoc)
* @see agentgui.core.charts.gui.ChartTab#replaceModel(agentgui.core.charts.DataModel)
*/
@Override
public void replaceModel(DataModel newModel) {
this.dataModel = newModel;

// this.chartPanel.setChart(ChartFactory.createTimeSeriesChart(
// this.dataModel.getOntologyModel().getChartSettings().getChartTitle(),
// this.dataModel.getOntologyModel().getChartSettings().getXAxisLabel(),
// this.dataModel.getOntologyModel().getChartSettings().getYAxisLabel(),
// ((TimeSeriesDataModel)this.dataModel).getTimeSeriesChartModel().getTimeSeriesCollection(),
// true, false, false
// ));
//
// applySettings();

TimeSeriesCollection tsc = ((TimeSeriesDataModel)this.dataModel).getTimeSeriesChartModel().getTimeSeriesCollection();
this.chartPanel.getChart().getXYPlot().setDataset(tsc);

dataModel.getChartModel().addObserver(this);
}

Expand All @@ -99,19 +86,19 @@ public void replaceModel(DataModel newModel) {
protected void applySettings() {
super.applySettings();
DateAxis da = (DateAxis) this.chartPanel.getChart().getXYPlot().getDomainAxis();

DateFormat dateFormat = new SimpleDateFormat(((TimeSeriesDataModel)dataModel).getTimeFormat());
da.setDateFormatOverride(dateFormat);
String formatString = ((TimeSeriesDataModel)dataModel).getTimeFormat();
TimeZoneDateFormat tzdf = new TimeZoneDateFormat(formatString, GlobalInfo.getCurrentZoneId());
da.setDateFormatOverride(tzdf);
}

/**
* Sets the time format for the time axis label ticks
* @param timeFormat
*/
void setTimeFormat(String timeFormat){
DateFormat dateFormat = new SimpleDateFormat(timeFormat);
TimeZoneDateFormat tzdf = new TimeZoneDateFormat(timeFormat, GlobalInfo.getCurrentZoneId());
DateAxis da = (DateAxis) this.chartPanel.getChart().getXYPlot().getDomainAxis();
da.setDateFormatOverride(dateFormat);
da.setDateFormatOverride(tzdf);
}

}

0 comments on commit 20f602d

Please sign in to comment.