Skip to content

Commit 1a65c00

Browse files
committed
Change type of timestamp fields in JobExecution/StepExecution to LocalDateTime
This commit changes the type of fields `startTime`, `endTime`, `createTime` and `lastUpdated` in `JobExecution` and `StepExecution` from `java.util.Date` to `java.time.LocalDateTime`. Resolves #1014
1 parent 4bcc6b1 commit 1a65c00

27 files changed

+223
-193
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
import java.io.IOException;
2020
import java.io.ObjectInputStream;
21+
import java.time.LocalDateTime;
2122
import java.util.ArrayList;
2223
import java.util.Collection;
2324
import java.util.Collections;
24-
import java.util.Date;
2525
import java.util.HashSet;
2626
import java.util.LinkedHashSet;
2727
import java.util.List;
@@ -52,13 +52,13 @@ public class JobExecution extends Entity {
5252

5353
private volatile BatchStatus status = BatchStatus.STARTING;
5454

55-
private volatile Date startTime = null;
55+
private volatile LocalDateTime startTime = null;
5656

57-
private volatile Date createTime = new Date(System.currentTimeMillis());
57+
private volatile LocalDateTime createTime = LocalDateTime.now();
5858

59-
private volatile Date endTime = null;
59+
private volatile LocalDateTime endTime = null;
6060

61-
private volatile Date lastUpdated = null;
61+
private volatile LocalDateTime lastUpdated = null;
6262

6363
private volatile ExitStatus exitStatus = ExitStatus.UNKNOWN;
6464

@@ -140,7 +140,7 @@ public JobParameters getJobParameters() {
140140
* @return The current end time.
141141
*/
142142
@Nullable
143-
public Date getEndTime() {
143+
public LocalDateTime getEndTime() {
144144
return endTime;
145145
}
146146

@@ -154,25 +154,25 @@ public void setJobInstance(JobInstance jobInstance) {
154154

155155
/**
156156
* Set the end time.
157-
* @param endTime The {@link Date} to be used for the end time.
157+
* @param endTime The {@link LocalDateTime} to be used for the end time.
158158
*/
159-
public void setEndTime(Date endTime) {
159+
public void setEndTime(LocalDateTime endTime) {
160160
this.endTime = endTime;
161161
}
162162

163163
/**
164164
* @return The current start time.
165165
*/
166166
@Nullable
167-
public Date getStartTime() {
167+
public LocalDateTime getStartTime() {
168168
return startTime;
169169
}
170170

171171
/**
172172
* Set the start time.
173-
* @param startTime The {@link Date} to be used for the start time.
173+
* @param startTime The {@link LocalDateTime} to be used for the start time.
174174
*/
175-
public void setStartTime(Date startTime) {
175+
public void setStartTime(LocalDateTime startTime) {
176176
this.startTime = startTime;
177177
}
178178

@@ -290,14 +290,14 @@ public ExecutionContext getExecutionContext() {
290290
/**
291291
* @return the time when this execution was created.
292292
*/
293-
public Date getCreateTime() {
293+
public LocalDateTime getCreateTime() {
294294
return createTime;
295295
}
296296

297297
/**
298298
* @param createTime The creation time of this execution.
299299
*/
300-
public void setCreateTime(Date createTime) {
300+
public void setCreateTime(LocalDateTime createTime) {
301301
this.createTime = createTime;
302302
}
303303

@@ -313,20 +313,20 @@ void addStepExecution(StepExecution stepExecution) {
313313
/**
314314
* Get the date representing the last time this {@code JobExecution} was updated in
315315
* the {@link org.springframework.batch.core.repository.JobRepository}.
316-
* @return a {@code Date} object representing the last time this {@code JobExecution}
317-
* was updated.
316+
* @return a {@link LocalDateTime} object representing the last time this
317+
* {@code JobExecution} was updated.
318318
*/
319319
@Nullable
320-
public Date getLastUpdated() {
320+
public LocalDateTime getLastUpdated() {
321321
return lastUpdated;
322322
}
323323

324324
/**
325325
* Set the last time this {@code JobExecution} was updated.
326-
* @param lastUpdated The {@link Date} instance to which to set the job execution's
327-
* {@code lastUpdated} attribute.
326+
* @param lastUpdated The {@link LocalDateTime} instance to which to set the job
327+
* execution's {@code lastUpdated} attribute.
328328
*/
329-
public void setLastUpdated(Date lastUpdated) {
329+
public void setLastUpdated(LocalDateTime lastUpdated) {
330330
this.lastUpdated = lastUpdated;
331331
}
332332

spring-batch-core/src/main/java/org/springframework/batch/core/StepExecution.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import java.io.IOException;
2020
import java.io.ObjectInputStream;
21+
import java.time.LocalDateTime;
2122
import java.util.ArrayList;
22-
import java.util.Date;
2323
import java.util.List;
2424
import java.util.concurrent.CopyOnWriteArrayList;
2525

@@ -61,13 +61,13 @@ public class StepExecution extends Entity {
6161

6262
private volatile long writeSkipCount = 0;
6363

64-
private volatile Date startTime = null;
64+
private volatile LocalDateTime startTime = null;
6565

66-
private volatile Date createTime = new Date(System.currentTimeMillis());
66+
private volatile LocalDateTime createTime = LocalDateTime.now();
6767

68-
private volatile Date endTime = null;
68+
private volatile LocalDateTime endTime = null;
6969

70-
private volatile Date lastUpdated = null;
70+
private volatile LocalDateTime lastUpdated = null;
7171

7272
private volatile ExecutionContext executionContext = new ExecutionContext();
7373

@@ -156,15 +156,15 @@ public void setCommitCount(long commitCount) {
156156
* @return the time when this execution ended or {@code null} if the step is running.
157157
*/
158158
@Nullable
159-
public Date getEndTime() {
159+
public LocalDateTime getEndTime() {
160160
return endTime;
161161
}
162162

163163
/**
164164
* Sets the time when this execution ended.
165165
* @param endTime The time when this execution ended.
166166
*/
167-
public void setEndTime(Date endTime) {
167+
public void setEndTime(LocalDateTime endTime) {
168168
this.endTime = endTime;
169169
}
170170

@@ -236,15 +236,15 @@ public void setRollbackCount(long rollbackCount) {
236236
* Gets the time this execution was created
237237
* @return the time when this execution was created.
238238
*/
239-
public Date getCreateTime() {
239+
public LocalDateTime getCreateTime() {
240240
return createTime;
241241
}
242242

243243
/**
244244
* Sets the time this execution was created
245245
* @param createTime creation time of this execution.
246246
*/
247-
public void setCreateTime(Date createTime) {
247+
public void setCreateTime(LocalDateTime createTime) {
248248
this.createTime = createTime;
249249
}
250250

@@ -253,15 +253,15 @@ public void setCreateTime(Date createTime) {
253253
* @return the time when this execution started.
254254
*/
255255
@Nullable
256-
public Date getStartTime() {
256+
public LocalDateTime getStartTime() {
257257
return startTime;
258258
}
259259

260260
/**
261261
* Sets the time when this execution started.
262262
* @param startTime The time when this execution started.
263263
*/
264-
public void setStartTime(Date startTime) {
264+
public void setStartTime(LocalDateTime startTime) {
265265
this.startTime = startTime;
266266
}
267267

@@ -457,16 +457,16 @@ public void setProcessSkipCount(long processSkipCount) {
457457
* @return the Date representing the last time this execution was persisted.
458458
*/
459459
@Nullable
460-
public Date getLastUpdated() {
460+
public LocalDateTime getLastUpdated() {
461461
return lastUpdated;
462462
}
463463

464464
/**
465465
* Sets the time when the {@code StepExecution} was last updated before persisting.
466-
* @param lastUpdated the {@link Date} instance used to establish the last updated
467-
* date for the {@code StepExecution}.
466+
* @param lastUpdated the {@link LocalDateTime} instance used to establish the last
467+
* updated date for the {@code StepExecution}.
468468
*/
469-
public void setLastUpdated(Date lastUpdated) {
469+
public void setLastUpdated(LocalDateTime lastUpdated) {
470470
this.lastUpdated = lastUpdated;
471471
}
472472

spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.batch.core.job;
1818

19+
import java.time.LocalDateTime;
1920
import java.util.Collection;
20-
import java.util.Date;
2121
import java.util.List;
2222
import java.util.stream.Collectors;
2323

@@ -296,7 +296,7 @@ public final void execute(JobExecution execution) {
296296

297297
if (execution.getStatus() != BatchStatus.STOPPING) {
298298

299-
execution.setStartTime(new Date());
299+
execution.setStartTime(LocalDateTime.now());
300300
updateStatus(execution, BatchStatus.STARTED);
301301

302302
listener.beforeJob(execution);
@@ -352,7 +352,7 @@ public final void execute(JobExecution execution) {
352352
}
353353
stopObservation(execution, observation);
354354
longTaskTimerSample.stop();
355-
execution.setEndTime(new Date());
355+
execution.setEndTime(LocalDateTime.now());
356356

357357
try {
358358
listener.afterJob(execution);

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package org.springframework.batch.core.launch.support;
1717

18+
import java.time.LocalDateTime;
1819
import java.util.ArrayList;
19-
import java.util.Date;
2020
import java.util.LinkedHashMap;
2121
import java.util.LinkedHashSet;
2222
import java.util.List;
@@ -447,7 +447,7 @@ public JobExecution abandon(long jobExecutionId)
447447
logger.info("Aborting job execution: " + jobExecution);
448448
}
449449
jobExecution.upgradeStatus(BatchStatus.ABANDONED);
450-
jobExecution.setEndTime(new Date());
450+
jobExecution.setEndTime(LocalDateTime.now());
451451
jobRepository.update(jobExecution);
452452

453453
return jobExecution;

spring-batch-core/src/main/java/org/springframework/batch/core/observability/BatchMetrics.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package org.springframework.batch.core.observability;
1717

1818
import java.time.Duration;
19+
import java.time.LocalDateTime;
1920
import java.util.Arrays;
20-
import java.util.Date;
2121
import java.util.concurrent.TimeUnit;
2222

2323
import io.micrometer.core.instrument.LongTaskTimer;
@@ -142,11 +142,11 @@ public static LongTaskTimer createLongTaskTimer(String name, String description,
142142
* @return the duration between start time and end time
143143
*/
144144
@Nullable
145-
public static Duration calculateDuration(@Nullable Date startTime, @Nullable Date endTime) {
145+
public static Duration calculateDuration(@Nullable LocalDateTime startTime, @Nullable LocalDateTime endTime) {
146146
if (startTime == null || endTime == null) {
147147
return null;
148148
}
149-
return Duration.between(startTime.toInstant(), endTime.toInstant());
149+
return Duration.between(startTime, endTime);
150150
}
151151

152152
/**

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import java.sql.ResultSet;
2020
import java.sql.SQLException;
21+
import java.sql.Timestamp;
2122
import java.sql.Types;
23+
import java.time.LocalDateTime;
2224
import java.util.HashMap;
2325
import java.util.HashSet;
2426
import java.util.List;
@@ -165,10 +167,16 @@ public void saveJobExecution(JobExecution jobExecution) {
165167
jobExecution.incrementVersion();
166168

167169
jobExecution.setId(jobExecutionIncrementer.nextLongValue());
168-
Object[] parameters = new Object[] { jobExecution.getId(), jobExecution.getJobId(), jobExecution.getStartTime(),
169-
jobExecution.getEndTime(), jobExecution.getStatus().toString(),
170-
jobExecution.getExitStatus().getExitCode(), jobExecution.getExitStatus().getExitDescription(),
171-
jobExecution.getVersion(), jobExecution.getCreateTime(), jobExecution.getLastUpdated() };
170+
Timestamp startTime = jobExecution.getStartTime() == null ? null
171+
: Timestamp.valueOf(jobExecution.getStartTime());
172+
Timestamp endTime = jobExecution.getEndTime() == null ? null : Timestamp.valueOf(jobExecution.getEndTime());
173+
Timestamp createTime = jobExecution.getCreateTime() == null ? null
174+
: Timestamp.valueOf(jobExecution.getCreateTime());
175+
Timestamp lastUpdated = jobExecution.getLastUpdated() == null ? null
176+
: Timestamp.valueOf(jobExecution.getLastUpdated());
177+
Object[] parameters = new Object[] { jobExecution.getId(), jobExecution.getJobId(), startTime, endTime,
178+
jobExecution.getStatus().toString(), jobExecution.getExitStatus().getExitCode(),
179+
jobExecution.getExitStatus().getExitDescription(), jobExecution.getVersion(), createTime, lastUpdated };
172180
getJdbcTemplate().update(getQuery(SAVE_JOB_EXECUTION), parameters,
173181
new int[] { Types.BIGINT, Types.BIGINT, Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR,
174182
Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP, Types.TIMESTAMP });
@@ -217,10 +225,16 @@ public void updateJobExecution(JobExecution jobExecution) {
217225
logger.debug("Truncating long message before update of JobExecution: " + jobExecution);
218226
}
219227
}
220-
Object[] parameters = new Object[] { jobExecution.getStartTime(), jobExecution.getEndTime(),
221-
jobExecution.getStatus().toString(), jobExecution.getExitStatus().getExitCode(), exitDescription,
222-
version, jobExecution.getCreateTime(), jobExecution.getLastUpdated(), jobExecution.getId(),
223-
jobExecution.getVersion() };
228+
Timestamp startTime = jobExecution.getStartTime() == null ? null
229+
: Timestamp.valueOf(jobExecution.getStartTime());
230+
Timestamp endTime = jobExecution.getEndTime() == null ? null : Timestamp.valueOf(jobExecution.getEndTime());
231+
Timestamp createTime = jobExecution.getCreateTime() == null ? null
232+
: Timestamp.valueOf(jobExecution.getCreateTime());
233+
Timestamp lastUpdated = jobExecution.getLastUpdated() == null ? null
234+
: Timestamp.valueOf(jobExecution.getLastUpdated());
235+
Object[] parameters = new Object[] { startTime, endTime, jobExecution.getStatus().toString(),
236+
jobExecution.getExitStatus().getExitCode(), exitDescription, version, createTime, lastUpdated,
237+
jobExecution.getId(), jobExecution.getVersion() };
224238

225239
// Check if given JobExecution's Id already exists, if none is found
226240
// it
@@ -433,12 +447,12 @@ public JobExecution mapRow(ResultSet rs, int rowNum) throws SQLException {
433447
jobExecution = new JobExecution(jobInstance, id, jobParameters);
434448
}
435449

436-
jobExecution.setStartTime(rs.getTimestamp(2));
437-
jobExecution.setEndTime(rs.getTimestamp(3));
450+
jobExecution.setStartTime(rs.getObject(2, LocalDateTime.class));
451+
jobExecution.setEndTime(rs.getObject(3, LocalDateTime.class));
438452
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(4)));
439453
jobExecution.setExitStatus(new ExitStatus(rs.getString(5), rs.getString(6)));
440-
jobExecution.setCreateTime(rs.getTimestamp(7));
441-
jobExecution.setLastUpdated(rs.getTimestamp(8));
454+
jobExecution.setCreateTime(rs.getObject(7, LocalDateTime.class));
455+
jobExecution.setLastUpdated(rs.getObject(8, LocalDateTime.class));
442456
jobExecution.setVersion(rs.getInt(9));
443457
return jobExecution;
444458
}

0 commit comments

Comments
 (0)