Browse Source

Parse build.time as an ISO 8601 instant

Closes gh-12420
pull/12419/merge
Andy Wilkinson 8 years ago
parent
commit
87239ba6c9
  1. 10
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/info/BuildProperties.java
  2. 11
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/info/BuildPropertiesTests.java

10
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/info/BuildProperties.java

@ -16,9 +16,9 @@ @@ -16,9 +16,9 @@
package org.springframework.boot.info;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
/**
@ -89,12 +89,12 @@ public class BuildProperties extends InfoProperties { @@ -89,12 +89,12 @@ public class BuildProperties extends InfoProperties {
private static void coerceDate(Properties properties, String key) {
String value = properties.getProperty(key);
if (value != null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
String updatedValue = String.valueOf(format.parse(value).getTime());
String updatedValue = String.valueOf(DateTimeFormatter.ISO_INSTANT
.parse(value, Instant::from).toEpochMilli());
properties.setProperty(key, updatedValue);
}
catch (ParseException ex) {
catch (DateTimeException ex) {
// Ignore and store the original value
}
}

11
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/info/BuildPropertiesTests.java

@ -16,6 +16,8 @@ @@ -16,6 +16,8 @@
package org.springframework.boot.info;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import org.junit.Test;
@ -31,14 +33,15 @@ public class BuildPropertiesTests { @@ -31,14 +33,15 @@ public class BuildPropertiesTests {
@Test
public void basicInfo() {
Instant instant = Instant.now();
BuildProperties properties = new BuildProperties(createProperties("com.example",
"demo", "0.0.1", "2016-03-04T14:36:33+0100"));
"demo", "0.0.1", DateTimeFormatter.ISO_INSTANT.format(instant)));
assertThat(properties.getGroup()).isEqualTo("com.example");
assertThat(properties.getArtifact()).isEqualTo("demo");
assertThat(properties.getVersion()).isEqualTo("0.0.1");
assertThat(properties.getTime()).isNotNull();
assertThat(properties.get("time")).isEqualTo("1457098593000");
assertThat(properties.getTime().toEpochMilli()).isEqualTo(1457098593000L);
assertThat(properties.getTime()).isEqualTo(instant);
assertThat(properties.get("time"))
.isEqualTo(String.valueOf(instant.toEpochMilli()));
}
@Test

Loading…
Cancel
Save