Skip to content

Commit 0ed5bec

Browse files
committed
Exchanged SimpleDateFormat through TimeZoneDateFormat where possible
1 parent b0f681c commit 0ed5bec

File tree

7 files changed

+34
-22
lines changed

7 files changed

+34
-22
lines changed

eclipseProjects/org.agentgui/bundles/de.enflexit.common/src/de/enflexit/common/swing/TimeZoneDateFormat.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.text.DateFormatSymbols;
55
import java.text.SimpleDateFormat;
66
import java.time.ZoneId;
7+
import java.util.Date;
78
import java.util.Locale;
89
import java.util.TimeZone;
910

@@ -101,7 +102,7 @@ public TimeZoneDateFormat(String pattern, ZoneId zoneId) {
101102
this.setZoneId(zoneId);
102103
}
103104

104-
105+
105106
/**
106107
* Sets the {@link ZoneId}.
107108
* @param zoneId the new zone id
@@ -116,5 +117,16 @@ public void setZoneId(ZoneId zoneId) {
116117
public ZoneId getZoneId() {
117118
return this.getTimeZone().toZoneId();
118119
}
120+
121+
122+
/**
123+
* Formats the specified UTC time stamp into a date-time string.
124+
*
125+
* @param utcTimeStamp the UTC time stamp value to be formatted into a date-time string.
126+
* @return the formatted date-time string.
127+
*/
128+
public String format(long utcTimeStamp) {
129+
return this.format(new Date(utcTimeStamp));
130+
}
119131

120132
}

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/core/gui/options/UpdateOptions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ private void setGlobalData2Form() {
348348
// --- Set when the update check was done the last time -----
349349
String titlePrefix = Language.translate("Nach Updates suchen");
350350
long dateChecked = Application.getGlobalInfo().getUpdateDateLastChecked();
351-
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy HH:mm:ss");
352-
this.getJLabelUpdateTitle().setText("<html><b>" + titlePrefix + ":</b> (Last check: " + sdf.format(new Date(dateChecked)) + ")</html>");
351+
this.getJLabelUpdateTitle().setText("<html><b>" + titlePrefix + ":</b> (Last check: " + new SimpleDateFormat("dd.MM.yy HH:mm:ss").format(new Date(dateChecked)) + ")</html>");
353352

354353
}
355354

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/core/update/ProjectRepositoryExport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ public static String getVersionQualifierForTimeStamp(long timeStamp) {
242242
* @return the version qualifier for time stamp
243243
*/
244244
public static String getVersionQualifierForDate(Date currTime) {
245-
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmm");
246-
return sdf.format(currTime);
245+
return new SimpleDateFormat("yyyyMMdd-HHmm").format(currTime);
247246
}
248247

249248
}

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/simulationService/time/TimeFormat.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public class TimeFormat implements Serializable {
4141

4242
private static final long serialVersionUID = 6691784575006563592L;
4343

44-
private String format = null;
45-
private Date displayFormat = null;
44+
private String format;
45+
private Date dateDisplay;
4646

4747

4848
/**
@@ -66,10 +66,10 @@ public String toString() {
6666
* @return the date to display
6767
*/
6868
private Date getDateDisplay() {
69-
if (displayFormat==null) {
70-
displayFormat = new Date();
69+
if (dateDisplay==null) {
70+
dateDisplay = new Date();
7171
}
72-
return displayFormat;
72+
return dateDisplay;
7373
}
7474

7575
/**

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/simulationService/time/TimeModelBaseExecutionElements.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
import java.awt.Font;
3333
import java.awt.event.ActionEvent;
3434
import java.awt.event.ActionListener;
35-
import java.text.SimpleDateFormat;
36-
import java.util.Date;
35+
import java.time.ZoneId;
3736
import java.util.Vector;
3837

3938
import javax.swing.BorderFactory;
@@ -45,9 +44,10 @@
4544
import javax.swing.border.EtchedBorder;
4645

4746
import agentgui.core.application.Language;
47+
import de.enflexit.common.swing.TimeZoneDateFormat;
4848

4949
/**
50-
* The Class JToolBarElements4TimeModelExecution has to be extended in order to
50+
* The Class TimeModelBaseExecutionElements has to be extended in order to
5151
* provide a specific display for a TimeModel during the execution of an agency.
5252
*
5353
* @see TimeModel
@@ -172,18 +172,20 @@ protected void removeAllSeparator() {
172172
* Returns a time formatted.
173173
*
174174
* @param time the time
175+
* @param zoneId the ZoneId
175176
* @param timeFormat the time format
176-
* @return the time formated
177+
* @return the time formatted
177178
*/
178-
protected String getTimeFormatted(long time, String timeFormat) {
179+
protected String getTimeFormatted(long time, ZoneId zoneId, String timeFormat) {
180+
181+
if (zoneId==null) zoneId = ZoneId.systemDefault();
182+
if (timeFormat==null || timeFormat.isBlank()) timeFormat = TimeModelDateBased.DEFAULT_TIME_FORMAT;
183+
179184
String timeString = null;
180185
try {
181-
timeString = new SimpleDateFormat(timeFormat).format(new Date(time));
186+
timeString = new TimeZoneDateFormat(timeFormat, zoneId).format(time);
182187
} catch (Exception ex) {
183-
// Retry below
184-
}
185-
if (timeString==null) {
186-
timeString = new SimpleDateFormat(TimeModelDateBased.DEFAULT_TIME_FORMAT).format(new Date(time));
188+
ex.printStackTrace();
187189
}
188190
return timeString;
189191
}

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/simulationService/time/TimeModelContinuousExecutionElements.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void setTimeModel(TimeModel timeModel) {
103103

104104
default:
105105
// --- timer view -----------------------------------
106-
this.getJLabelTimeDisplay().setText(this.getTimeFormatted(time, timeFormat));
106+
this.getJLabelTimeDisplay().setText(this.getTimeFormatted(time, this.timeModelContinuous.getZoneId(), timeFormat));
107107
this.getJButtonTimeConfig().setText(this.getJMenuItemViewTimer().getText());
108108
break;
109109
}

eclipseProjects/org.agentgui/bundles/org.agentgui.core/src/agentgui/simulationService/time/TimeModelDiscreteExecutionElements.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void setTimeModel(TimeModel timeModel) {
9797

9898
default:
9999
// --- timer view -------------------------
100-
this.getJLabelTimeDisplay().setText(this.getTimeFormatted(time, timeFormat));
100+
this.getJLabelTimeDisplay().setText(this.getTimeFormatted(time, this.timeModelDiscrete.getZoneId(), timeFormat));
101101
this.getJButtonTimeConfig().setText(this.getJMenuItemViewTimer().getText());
102102
break;
103103
}

0 commit comments

Comments
 (0)