Skip to content

Commit 04d4868

Browse files
JonCookpivovarit
authored andcommitted
BAEL-1849 - Convert from String to Date in Java (eugenp#4476)
1 parent 90c0268 commit 04d4868

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.baeldung.date;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
6+
import java.text.ParseException;
7+
import java.text.SimpleDateFormat;
8+
import java.time.LocalDate;
9+
import java.time.LocalDateTime;
10+
import java.time.ZoneId;
11+
import java.time.ZonedDateTime;
12+
import java.time.format.DateTimeFormatter;
13+
import java.time.format.DateTimeParseException;
14+
import java.util.Calendar;
15+
import java.util.Date;
16+
import java.util.GregorianCalendar;
17+
import java.util.Locale;
18+
19+
import org.apache.commons.lang3.time.DateUtils;
20+
import org.joda.time.DateTime;
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.rules.ExpectedException;
24+
25+
public class StringToDateUnitTest {
26+
27+
@Rule
28+
public ExpectedException thrown = ExpectedException.none();
29+
30+
@Test
31+
public void givenDateString_whenConvertedToDate_thenWeGetCorrectLocalDate() {
32+
LocalDate expectedLocalDate = LocalDate.of(2018, 05, 05);
33+
34+
LocalDate date = LocalDate.parse("2018-05-05");
35+
36+
assertThat(date).isEqualTo(expectedLocalDate);
37+
}
38+
39+
@Test
40+
public void givenDateString_whenConvertedToDate_thenWeGetCorrectLocalDateTime() {
41+
LocalDateTime expectedLocalDateTime = LocalDateTime.of(2018, 05, 05, 11, 50, 55);
42+
43+
LocalDateTime dateTime = LocalDateTime.parse("2018-05-05T11:50:55");
44+
45+
assertThat(dateTime).isEqualTo(expectedLocalDateTime);
46+
}
47+
48+
@Test
49+
public void givenDateString_whenConvertedToDate_thenWeGetDateTimeParseException() {
50+
thrown.expect(DateTimeParseException.class);
51+
thrown.expectMessage("Text '2018-05-05' could not be parsed at index 10");
52+
53+
LocalDateTime.parse("2018-05-05");
54+
}
55+
56+
@Test
57+
public void givenDateString_whenConvertedToDate_thenWeGetCorrectZonedDateTime() {
58+
LocalDateTime localDateTime = LocalDateTime.of(2015, 05, 05, 10, 15, 30);
59+
ZonedDateTime expectedZonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("Europe/Paris"));
60+
61+
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2015-05-05T10:15:30+01:00[Europe/Paris]");
62+
63+
assertThat(zonedDateTime).isEqualTo(expectedZonedDateTime);
64+
}
65+
66+
@Test
67+
public void givenDateString_whenConvertedToDateUsingFormatter_thenWeGetCorrectLocalDate() {
68+
LocalDate expectedLocalDate = LocalDate.of(1959, 7, 9);
69+
70+
String dateInString = "19590709";
71+
LocalDate date = LocalDate.parse(dateInString, DateTimeFormatter.BASIC_ISO_DATE);
72+
73+
assertThat(date).isEqualTo(expectedLocalDate);
74+
}
75+
76+
@Test
77+
public void givenDateString_whenConvertedToDateUsingCustomFormatter_thenWeGetCorrectLocalDate() {
78+
LocalDate expectedLocalDate = LocalDate.of(1980, 05, 05);
79+
80+
String dateInString = "Mon, 05 May 1980";
81+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy", Locale.ENGLISH);
82+
LocalDate dateTime = LocalDate.parse(dateInString, formatter);
83+
84+
assertThat(dateTime).isEqualTo(expectedLocalDate);
85+
}
86+
87+
@Test
88+
public void givenDateString_whenConvertedToDate_thenWeGetCorrectDate() throws ParseException {
89+
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
90+
91+
String dateInString = "7-Jun-2013";
92+
Date date = formatter.parse(dateInString);
93+
94+
assertDateIsCorrect(date);
95+
}
96+
97+
@Test
98+
public void givenDateString_whenConvertedToDate_thenWeGetParseException() throws ParseException {
99+
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
100+
101+
thrown.expect(ParseException.class);
102+
thrown.expectMessage("Unparseable date: \"07/06/2013\"");
103+
104+
String dateInString = "07/06/2013";
105+
formatter.parse(dateInString);
106+
}
107+
108+
@Test
109+
public void givenDateString_whenConvertedToDate_thenWeGetCorrectJodaDateTime() {
110+
org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
111+
112+
String dateInString = "07/06/2013 10:11:59";
113+
DateTime dateTime = DateTime.parse(dateInString, formatter);
114+
115+
assertEquals("Day of Month should be 7: ", 7, dateTime.getDayOfMonth());
116+
assertEquals("Month should be: ", 6, dateTime.getMonthOfYear());
117+
assertEquals("Year should be: ", 2013, dateTime.getYear());
118+
119+
assertEquals("Hour of day should be: ", 10, dateTime.getHourOfDay());
120+
assertEquals("Minutes of hour should be: ", 11, dateTime.getMinuteOfHour());
121+
assertEquals("Seconds of minute should be: ", 59, dateTime.getSecondOfMinute());
122+
}
123+
124+
@Test
125+
public void givenDateString_whenConvertedToDate_thenWeGetCorrectDateTime() throws ParseException {
126+
String dateInString = "07/06-2013";
127+
Date date = DateUtils.parseDate(dateInString, new String[] { "yyyy-MM-dd HH:mm:ss", "dd/MM-yyyy" });
128+
129+
assertDateIsCorrect(date);
130+
}
131+
132+
private void assertDateIsCorrect(Date date) {
133+
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
134+
calendar.setTime(date);
135+
136+
assertEquals("Day of Month should be 7: ", 7, calendar.get(Calendar.DAY_OF_MONTH));
137+
assertEquals("Month should be: ", 5, calendar.get(Calendar.MONTH));
138+
assertEquals("Year should be: ", 2013, calendar.get(Calendar.YEAR));
139+
}
140+
141+
}

0 commit comments

Comments
 (0)