Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf201134

Browse files
committed
Add string to date snippet
1 parentaf25526 commitf201134

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+
publicstaticDate stringToDate(String date,String format) throwsParseException {
135+
SimpleDateFormat simpleDateFormat=newSimpleDateFormat(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
importjava.io.File;
55
importjava.io.IOException;
66
importjava.nio.file.Files;
7+
importjava.text.ParseException;
8+
importjava.text.SimpleDateFormat;
79
importjava.util.Arrays;
10+
importjava.util.Date;
811
importjava.util.List;
912

1013
/*
@@ -106,4 +109,16 @@ public static void captureScreen(String filename) throws AWTException, IOExcepti
106109
BufferedImageimage =robot.createScreenCapture(screenRectangle);
107110
ImageIO.write(image,"png",newFile(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+
publicstaticDatestringToDate(Stringdate,Stringformat)throwsParseException {
121+
SimpleDateFormatsimpleDateFormat =newSimpleDateFormat(format);
122+
returnsimpleDateFormat.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
importjava.io.File;
55
importjava.io.IOException;
66
importjava.nio.file.Files;
7+
importjava.text.ParseException;
8+
importjava.util.Calendar;
9+
importjava.util.Date;
710
importjava.util.List;
811

912
importstaticorg.junit.Assert.*;
@@ -125,4 +128,15 @@ public void testCaptureScreen() throws IOException, AWTException {
125128
Files.deleteIfExists(newFile(filename).toPath());
126129
}
127130
}
131+
/**
132+
* Tests for {@link Library#stringToDate(String, String)}
133+
*/
134+
@Test
135+
publicvoidtestStringToDate()throwsParseException {
136+
Calendarcalendar =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)

[8]ページ先頭

©2009-2025 Movatter.jp