'How to set current date in yaml file
I am creating a dataset in yml format to initialize a table in a postgresql database
- id : 5
description : "test5"
is_deleted : false
last_redaction_date : 2021-12-13 15:09:52.715570
persist_date : 2022-03-02 15:09:52.715570
title : "test5"
user_id : 15
How can I set the current date in the required format to the persist_date variable so as not to manually enter the date each time?
@AutoConfigureMockMvc
@SpringBootTest(classes = JmApplication.class)
public class TestQuestionResourceController extends AbstractClassForDRRiderMockMVCTests {
@Autowired
private MockMvc mvc;
@Test
@DataSet(cleanBefore = true,
value = {
"dataset/testQuestionResourceController/question_different_date.yml",
},
strategy = SeedStrategy.CLEAN_INSERT
)
public void getQuestionSortedByWeightForTheWeek() throws Exception {
Solution 1:[1]
You can get the current date and time from ZonedDateTime:
ZonedDateTime now = ZonedDateTime.now();
And then you can format the time using DateTimeFormatter's ISO_LOCAL_DATE and ISO_LOCAL_TIME:
String currentDateTime = DateTimeFormatter.ISO_LOCAL_DATE.format(now) +
DateTimeFormatter.ISO_LOCAL_TIME.format(now);
Which produces "2022-03-14 12:40:32.8071372".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Conrad |
