我在处理日期时遇到了弹簧数据休息的问题。简而言之,就是将日期推迟一天。例如,如果我有 1111-11-11,它会返回给我 1111-11-10。
SO(ex1,ex2,ex3)中有一些相关的帖子,但没有一个解决了问题。
我有一个实体LocalDate:
@Column(nullable=true)
private LocalDate birthDate;
Run Code Online (Sandbox Code Playgroud)
我也有这个存储库:
@RepositoryRestResource(collectionResourceRel = "person", path = "person")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long>{
}
Run Code Online (Sandbox Code Playgroud)
当我将 保存birthDate在数据库中时(我使用的是 MySQL),它会正确保存。但是,当我进行此查询时,例如:
Person p = personRepo.findById(1L).get();
Run Code Online (Sandbox Code Playgroud)
获取日期晚了一天(就像上面的例子一样)。我试图实现这个建议,即将 LocalDate 更改为 Date 并包含 Jackson 注释,但它不起作用。我也尝试包含jackson-modules-java8,但问题仍然存在。
现在最有趣的事情。因为我只是在测试,所以我包括了日期 1111-11-11。我将其更改为今天的日期 2019-02-06。然后,获取工作!这个时候,我想是不是很旧的日期有问题。因此,例如,我尝试了 1970-01-01,而春天又回到了 1969-12-31。我意识到如果我在数据库中包含 1986-01-01 以上的日期,一切正常。但是,如果我在此之下包含任何内容,我会迟到一天。
有人对这个问题有一些暗示吗?
感谢您的时间!
编辑: 我还检查了我的数据库时区,没关系!
+--------------------+---------------------+--------------------+
| @@GLOBAL.time_zone | @@session.time_zone | @@system_time_zone |
+--------------------+---------------------+--------------------+
| SYSTEM | SYSTEM | -02 |
+--------------------+---------------------+--------------------+
1 …Run Code Online (Sandbox Code Playgroud) 我在Spring应用程序中进行了一个简单的测试,其默认时区设置为UTC:
@SpringBootApplication
public class MemberIntegrationApp {
@Autowired
private TimeZoneProperties timeZoneProperties;
@PostConstruct
void started() {
TimeZone.setDefault(TimeZone.getTimeZone(timeZoneProperties.getAppDefault())); // which is UTC
}
public static void main(String[] args) {
SpringApplication.run(MemberIntegrationApp.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
并且,这个简单的测试:( 测试类注释@SpringBootTest用于在主类中加载配置并且@SpringRunner也被应用)
/**
* Test the effect of setting timezone
*/
@Test
public void testTimezoneSettingOnSimpleDateFormat() throws ParseException {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = "2018-08-08 12:34:56";
log.info("Trying to parse the date string: {}", d);
Date result = f.parse(d);
log.info("The result …Run Code Online (Sandbox Code Playgroud) 我想配置jackson为以以下格式输出任何日期/时间值:
spring.jackson.date-format=yyyy-MM-dd'T'HH:mm:ss
我正在获取许多数据库行,并将它们作为json地图返回。
@RestController
public class MyService {
@GetMapping
public List<Map<String, Object>> get(Param params) {
return jdbcTemplate.queryForList(sql, params);
}
}
Run Code Online (Sandbox Code Playgroud)
问题:数据库和jvm的默认时区为Europe/Berlin,因此为UTC + 2。因此,杰克逊会先自动将接收java.sql.Timestamp到的所有数据库转换为UTC(减去2小时),然后通过json输出它们。
在mysql数据库本身中,它是一种datetime类型。
但是我只想让杰克逊按原样输出时间戳,而无需事先转换!是否可以跳过时区校正?
我只想忽略时区而不进行交谈。剪下来
java ×3
spring-boot ×2
datetime ×1
jackson ×1
java-8 ×1
localdate ×1
spring ×1
spring-data ×1
spring-mvc ×1