File tree 4 files changed +62
-0
lines changed
Chapter03/BONUS_1_ConvertYearMonthToDate
src/main/java/modern/challenge
4 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Converting ` Iterable ` to ` List `
2
+ Write a program that converts an ` Iterable ` to ` List ` .
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3
+ <modelVersion >4.0.0</modelVersion >
4
+ <groupId >com.app</groupId >
5
+ <artifactId >BONUS_1_ConvertYearMonthToDate</artifactId >
6
+ <version >1.0-SNAPSHOT</version >
7
+ <packaging >jar</packaging >
8
+ <properties >
9
+ <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
10
+ <maven .compiler.source>13</maven .compiler.source>
11
+ <maven .compiler.target>13</maven .compiler.target>
12
+ </properties >
13
+ <name >BONUS_1_ConvertYearMonthToDate</name >
14
+ </project >
Original file line number Diff line number Diff line change
1
+ package modern .challenge ;
2
+
3
+ import java .time .YearMonth ;
4
+ import java .time .ZoneId ;
5
+ import java .util .Date ;
6
+
7
+ public class Converters {
8
+
9
+ private Converters () {
10
+ throw new AssertionError ("Cannot be instantiatied" );
11
+ }
12
+
13
+ public static YearMonth toYearMonth (Date date ) {
14
+
15
+ if (date == null ) {
16
+ throw new IllegalArgumentException ("The given date cannot be null" );
17
+ }
18
+
19
+ return YearMonth .from (date .toInstant ()
20
+ .atZone (ZoneId .systemDefault ())
21
+ .toLocalDate ());
22
+ }
23
+
24
+ public static Date toDate (YearMonth ym ) {
25
+
26
+ if (ym == null ) {
27
+ throw new IllegalArgumentException ("The given year-month cannot be null" );
28
+ }
29
+
30
+ return Date .from (ym .atDay (1 )
31
+ .atStartOfDay (ZoneId .systemDefault ()).toInstant ());
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ package modern .challenge
885F
span>;
2
+
3
+ import java .time .YearMonth ;
4
+ import java .util .Date ;
5
+
6
+ public class MainApplication {
7
+
8
+ public static void main (String [] args ) {
9
+
10
+ System .out .println ("Date to YearMonth: " + Converters .toYearMonth (new Date ()));
11
+ System .out .println ("YearMonth to Date: " + Converters .toDate (YearMonth .now ()));
12
+ }
13
+ }
You can’t perform that action at this time.
0 commit comments