-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathString_template_4.snippet
31 lines (31 loc) · 1.2 KB
/
String_template_4.snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Printing dates with FMT
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import static java.util.FormatProcessor.FMT;
record Price(int price) {}
record Rental(LocalDate start, LocalDate end, Price price) {
Rental(String start, String end, int price) {
this(
LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE),
LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE),
new Price(price));
}
}
public static String format(Rental rental) {
var formatter = DateTimeFormatter.ofPattern("MMMM dd yyyy");
return FMT."""
Rental from \{formatter.format(rental.start())}\
to \{formatter.format(rental.start())}, \
for the price \{rental.price().price()}""";
}
String format = "MMMM dd yyyy";
LocalDate today = LocalDate.now();
System.out.println(DateTimeFormatter.ofPattern("MMMM dd yyyy").format(today));
var rentals = List.of(
new Rental("2024-03-19", "2024-03-22", 800),
new Rental("2024-07-01", "2024-07-31", 4_000),
new Rental("2024-09-15", "2024-09-22", 2_500)
);
rentals.stream()
.map(rental -> format(rental))
.forEach(System.out::println);