Skip to content

Commit f201134

Browse files
committed
Add string to date snippet
1 parent af25526 commit f201134

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Update the sample application with the snippet and add a test for it. After prov
2525

2626
### String
2727
* [Reverse string](#reverse-string)
28+
* [String to date](#string-to-date)
2829

2930
## Array
3031

@@ -126,3 +127,14 @@ Update the sample application with the snippet and add a test for it. After prov
126127
```
127128

128129
[⬆ back to top](#table-of-contents)
130+
131+
### String to date
132+
133+
```java
134+
public static Date stringToDate(String date, String format) throws ParseException {
135+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
136+
return simpleDateFormat.parse(date);
137+
}
138+
```
139+
140+
[⬆ back to top](#table-of-contents)

src/main/java/Library.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import java.io.File;
55
import java.io.IOException;
66
import java.nio.file.Files;
7+
import java.text.ParseException;
8+
import java.text.SimpleDateFormat;
79
import java.util.Arrays;
10+
import java.util.Date;
811
import java.util.List;
912

1013
/*
@@ -106,4 +109,16 @@ public static void captureScreen(String filename) throws AWTException, IOExcepti
106109
BufferedImage image = robot.createScreenCapture(screenRectangle);
107110
ImageIO.write(image, "png", new File(filename));
108111
}
112+
113+
/**
114+
* Convert string to date
115+
* @param date the date string
116+
* @param format expected date format
117+
* @return Date
118+
* @throws ParseException
119+
*/
120+
public static Date stringToDate(String date, String format) throws ParseException {
121+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
122+
return simpleDateFormat.parse(date);
123+
}
109124
}

src/test/java/LibraryTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.io.File;
55
import java.io.IOException;
66
import java.nio.file.Files;
7+
import java.text.ParseException;
8+
import java.util.Calendar;
9+
import java.util.Date;
710
import java.util.List;
811

912
import static org.junit.Assert.*;
@@ -125,4 +128,15 @@ public void testCaptureScreen() throws IOException, AWTException {
125128
Files.deleteIfExists(new File(filename).toPath());
126129
}
127130
}
131+
/**
132+
* Tests for {@link Library#stringToDate(String, String)}
133+
*/
134+
@Test
135+
public void testStringToDate() throws ParseException {
136+
Calendar calendar = Calendar.getInstance();
137+
calendar.setTime(Library.stringToDate("2017-08-18", "yyyy-MM-dd"));
138+
assertEquals(2017, calendar.get(Calendar.YEAR));
139+
assertEquals(8, calendar.get(Calendar.MONTH) + 1);
140+
assertEquals(18, calendar.get(Calendar.DAY_OF_MONTH));
141+
}
128142
}

0 commit comments

Comments
 (0)