Skip to content

Commit 08b589e

Browse files
dkapilCalamarBicefalo
authored andcommitted
BAEL-1892 Get Integer Values from Date
-Added classes to show case how to extract values from classes Date and Calendar, and LocalDate, LocalDateTime, ZonedDateTime and OffSetDateTime -Added unit test cases for all classes
1 parent 287d0a0 commit 08b589e

10 files changed

Lines changed: 289 additions & 0 deletions
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.datetime;
2+
3+
import java.util.Calendar;
4+
import java.util.Date;
5+
6+
public class DateExtractYearMonthDayIntegerValues {
7+
8+
int getYear(Date date) {
9+
Calendar calendar = Calendar.getInstance();
10+
calendar.setTime(date);
11+
12+
return calendar.get(Calendar.YEAR);
13+
}
14+
15+
int getMonth(Date date) {
16+
Calendar calendar = Calendar.getInstance();
17+
calendar.setTime(date);
18+
19+
return calendar.get(Calendar.MONTH);
20+
}
21+
22+
int getDay(Date date) {
23+
Calendar calendar = Calendar.getInstance();
24+
calendar.setTime(date);
25+
26+
return calendar.get(Calendar.DAY_OF_MONTH);
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDate;
4+
5+
public class LocalDateExtractYearMonthDayIntegerValues {
6+
7+
int getYear(LocalDate localDate) {
8+
return localDate.getYear();
9+
}
10+
11+
int getMonth(LocalDate localDate) {
12+
return localDate.getMonthValue();
13+
}
14+
15+
int getDay(LocalDate localDate) {
16+
return localDate.getDayOfMonth();
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDateTime;
4+
5+
public class LocalDateTimeExtractYearMonthDayIntegerValues {
6+
7+
int getYear(LocalDateTime localDateTime) {
8+
return localDateTime.getYear();
9+
}
10+
11+
int getMonth(LocalDateTime localDateTime) {
12+
return localDateTime.getMonthValue();
13+
}
14+
15+
int getDay(LocalDateTime localDateTime) {
16+
return localDateTime.getDayOfMonth();
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.OffsetDateTime;
4+
5+
public class OffsetDateTimeExtractYearMonthDayIntegerValues {
6+
7+
int getYear(OffsetDateTime offsetDateTime) {
8+
return offsetDateTime.getYear();
9+
}
10+
11+
int getMonth(OffsetDateTime offsetDateTime) {
12+
return offsetDateTime.getMonthValue();
13+
}
14+
15+
int getDay(OffsetDateTime offsetDateTime) {
16+
return offsetDateTime.getDayOfMonth();
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.ZonedDateTime;
4+
5+
public class ZonedDateTimeExtractYearMonthDayIntegerValues {
6+
7+
int getYear(ZonedDateTime zonedDateTime) {
8+
return zonedDateTime.getYear();
9+
}
10+
11+
int getMonth(ZonedDateTime zonedDateTime) {
12+
return zonedDateTime.getMonthValue();
13+
}
14+
15+
int getDay(ZonedDateTime zonedDateTime) {
16+
return zonedDateTime.getDayOfMonth();
17+
}
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.datetime;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.text.ParseException;
7+
import java.text.SimpleDateFormat;
8+
import java.util.Date;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
public class DateExtractYearMonthDayIntegerValuesUnitTest {
14+
15+
DateExtractYearMonthDayIntegerValues extractYearMonthDateIntegerValues = new DateExtractYearMonthDayIntegerValues();
16+
17+
Date date;
18+
19+
@Before
20+
public void setup() throws ParseException
21+
{
22+
date=new SimpleDateFormat("dd-MM-yyyy").parse("01-03-2018");
23+
}
24+
25+
@Test
26+
public void whenGetYear_thenCorrectYear()
27+
{
28+
int actualYear=extractYearMonthDateIntegerValues.getYear(date);
29+
assertThat(actualYear,is(2018));
30+
}
31+
32+
@Test
33+
public void whenGetMonth_thenCorrectMonth()
34+
{
35+
int actualMonth=extractYearMonthDateIntegerValues.getMonth(date);
36+
assertThat(actualMonth,is(02));
37+
}
38+
39+
@Test
40+
public void whenGetDay_thenCorrectDay()
41+
{
42+
int actualDayOfMonth=extractYearMonthDateIntegerValues.getDay(date);
43+
assertThat(actualDayOfMonth,is(01));
44+
}
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.datetime;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.time.LocalDate;
7+
8+
import org.junit.Test;
9+
10+
public class LocalDateExtractYearMonthDayIntegerValuesUnitTest {
11+
12+
LocalDateExtractYearMonthDayIntegerValues localDateExtractYearMonthDayIntegerValues=new LocalDateExtractYearMonthDayIntegerValues();
13+
14+
LocalDate localDate=LocalDate.parse("2007-12-03");
15+
16+
@Test
17+
public void whenGetYear_thenCorrectYear()
18+
{
19+
int actualYear=localDateExtractYearMonthDayIntegerValues.getYear(localDate);
20+
assertThat(actualYear,is(2007));
21+
}
22+
23+
@Test
24+
public void whenGetMonth_thenCorrectMonth()
25+
{
26+
int actualMonth=localDateExtractYearMonthDayIntegerValues.getMonth(localDate);
27+
assertThat(actualMonth,is(12));
28+
}
29+
30+
@Test
31+
public void whenGetDay_thenCorrectDay()
32+
{
33+
int actualDayOfMonth=localDateExtractYearMonthDayIntegerValues.getDay(localDate);
34+
assertThat(actualDayOfMonth,is(03));
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.datetime;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.time.LocalDateTime;
7+
8+
import org.junit.Test;
9+
10+
public class LocalDateTimeExtractYearMonthDayIntegerValuesUnitTest {
11+
12+
LocalDateTimeExtractYearMonthDayIntegerValues localDateTimeExtractYearMonthDayIntegerValues = new LocalDateTimeExtractYearMonthDayIntegerValues();
13+
14+
LocalDateTime localDateTime=LocalDateTime.parse("2007-12-03T10:15:30");
15+
16+
@Test
17+
public void whenGetYear_thenCorrectYear()
18+
{
19+
int actualYear=localDateTimeExtractYearMonthDayIntegerValues.getYear(localDateTime);
20+
assertThat(actualYear,is(2007));
21+
}
22+
23+
@Test
24+
public void whenGetMonth_thenCorrectMonth()
25+
{
26+
int actualMonth=localDateTimeExtractYearMonthDayIntegerValues.getMonth(localDateTime);
27+
assertThat(actualMonth,is(12));
28+
}
29+
30+
@Test
31+
public void whenGetDay_thenCorrectDay()
32+
{
33+
int actualDayOfMonth=localDateTimeExtractYearMonthDayIntegerValues.getDay(localDateTime);
34+
assertThat(actualDayOfMonth,is(03));
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.datetime;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.time.OffsetDateTime;
7+
8+
import org.junit.Test;
9+
10+
public class OffsetDateTimeExtractYearMonthDayIntegerValuesUnitTest {
11+
12+
OffsetDateTimeExtractYearMonthDayIntegerValues offsetDateTimeExtractYearMonthDayIntegerValues = new OffsetDateTimeExtractYearMonthDayIntegerValues();
13+
14+
OffsetDateTime offsetDateTime=OffsetDateTime.parse("2007-12-03T10:15:30+01:00");
15+
16+
@Test
17+
public void whenGetYear_thenCorrectYear()
18+
{
19+
int actualYear=offsetDateTimeExtractYearMonthDayIntegerValues.getYear(offsetDateTime);
20+
assertThat(actualYear,is(2007));
21+
}
22+
23+
@Test
24+
public void whenGetMonth_thenCorrectMonth()
25+
{
26+
int actualMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getMonth(offsetDateTime);
27+
assertThat(actualMonth,is(12));
28+
}
29+
30+
@Test
31+
public void whenGetDay_thenCorrectDay()
32+
{
33+
int actualDayOfMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getDay(offsetDateTime);
34+
assertThat(actualDayOfMonth,is(03));
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.datetime;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.time.ZonedDateTime;
7+
8+
import org.junit.Test;
9+
10+
public class ZonedDateTimeExtractYearMonthDayIntegerValuesUnitTest {
11+
12+
ZonedDateTimeExtractYearMonthDayIntegerValues zonedDateTimeExtractYearMonthDayIntegerValues = new ZonedDateTimeExtractYearMonthDayIntegerValues();
13+
14+
ZonedDateTime zonedDateTime=ZonedDateTime.parse("2007-12-03T10:15:30+01:00");
15+
16+
@Test
17+
public void whenGetYear_thenCorrectYear()
18+
{
19+
int actualYear=zonedDateTimeExtractYearMonthDayIntegerValues.getYear(zonedDateTime);
20+
assertThat(actualYear,is(2007));
21+
}
22+
23+
@Test
24+
public void whenGetMonth_thenCorrectMonth()
25+
{
26+
int actualMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getMonth(zonedDateTime);
27+
assertThat(actualMonth,is(12));
28+
}
29+
30+
@Test
31+
public void whenGetDay_thenCorrectDay()
32+
{
33+
int actualDayOfMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getDay(zonedDateTime);
34+
assertThat(actualDayOfMonth,is(03));
35+
}
36+
}

0 commit comments

Comments
 (0)