Skip to content

Commit 21fb0aa

Browse files
committed
Test date format
1 parent e761534 commit 21fb0aa

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

code-samples-base/src/main/java/org/fugerit/java/code/samples/pdfbox2/AgeCheck.java renamed to code-samples-base/src/main/java/org/fugerit/java/code/samples/base/AgeCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.fugerit.java.code.samples.pdfbox2;
1+
package org.fugerit.java.code.samples.base;
22

33
import java.time.LocalDate;
44
import java.time.temporal.ChronoUnit;
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
package test.org.fugerit.java.code.samples.pdfbox2;
1+
package test.org.fugerit.java.code.samples.base;
22

33
import lombok.extern.slf4j.Slf4j;
4-
import org.fugerit.java.code.samples.pdfbox2.AgeCheck;
4+
import org.fugerit.java.code.samples.base.AgeCheck;
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.Test;
77

88
import java.time.LocalDate;
9+
import java.time.temporal.ChronoUnit;
910

1011
@Slf4j
1112
class TestAgeCheck {
1213

14+
private boolean testWorkerModule7( LocalDate d1, LocalDate d2 ) {
15+
boolean result = (ChronoUnit.DAYS.between( d1, d2 )%7==0);
16+
log.info( "d1 {}, d2 {}, res : {}", d1, d2, result );
17+
return result;
18+
}
19+
20+
@Test
21+
void testWeek() {
22+
LocalDate d2 = LocalDate.of( 2024, 8, 1 );
23+
Assertions.assertTrue( testWorkerModule7( LocalDate.of( 2024, 7, 25 ), d2 ) );
24+
Assertions.assertFalse( testWorkerModule7( LocalDate.of( 2024, 7, 26 ), d2 ) );
25+
}
26+
1327
private boolean check18Worker( LocalDate d ) {
1428
boolean test = AgeCheck.isAgeAtLeast18( d );
1529
log.info( "check date: {}, isAgeAtLeast18?: {}", d, test );
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package test.org.fugerit.java.code.samples.base;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.time.LocalDate;
9+
import java.time.format.DateTimeFormatter;
10+
11+
@Slf4j
12+
class TestConvertDate {
13+
14+
@Test
15+
void testConvert() throws JsonProcessingException {
16+
String json = "{\"birthDate\":\"1969-06-01T01:00:00\"}";
17+
ObjectMapper mapper = new ObjectMapper();
18+
TestModel testModel = mapper.readValue( json, TestModel.class );
19+
LocalDate birthDate = testModel.getBirthDate();
20+
log.info( "birthDate: {}", birthDate );
21+
}
22+
23+
}
24+
25+
@Slf4j
26+
class TestModel {
27+
28+
public void setBirthDate( String v ) {
29+
try {
30+
if ( v != null && v.length() >= 10 ) {
31+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
32+
String dv = v.substring( 0, 10 );
33+
log.info( "dv: {}", dv );
34+
this.birthDateD = LocalDate.parse(dv, formatter);
35+
}
36+
} catch (Exception e) {
37+
throw new RuntimeException( String.format( "Exception formatting birthdate %s", v ), e);
38+
}
39+
}
40+
41+
public LocalDate getBirthDate() {
42+
return this.birthDateD;
43+
}
44+
45+
private LocalDate birthDateD;
46+
47+
private String birthDate;
48+
49+
}

0 commit comments

Comments
 (0)