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
This commit is contained in:
Dhawal Kapil 2018-06-26 01:05:03 +05:30 committed by José Carlos Valero Sánchez
parent 287d0a062a
commit 08b589e8f6
10 changed files with 289 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package com.baeldung.datetime;
import java.util.Calendar;
import java.util.Date;
public class DateExtractYearMonthDayIntegerValues {
int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH);
}
int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.LocalDate;
public class LocalDateExtractYearMonthDayIntegerValues {
int getYear(LocalDate localDate) {
return localDate.getYear();
}
int getMonth(LocalDate localDate) {
return localDate.getMonthValue();
}
int getDay(LocalDate localDate) {
return localDate.getDayOfMonth();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
public class LocalDateTimeExtractYearMonthDayIntegerValues {
int getYear(LocalDateTime localDateTime) {
return localDateTime.getYear();
}
int getMonth(LocalDateTime localDateTime) {
return localDateTime.getMonthValue();
}
int getDay(LocalDateTime localDateTime) {
return localDateTime.getDayOfMonth();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.OffsetDateTime;
public class OffsetDateTimeExtractYearMonthDayIntegerValues {
int getYear(OffsetDateTime offsetDateTime) {
return offsetDateTime.getYear();
}
int getMonth(OffsetDateTime offsetDateTime) {
return offsetDateTime.getMonthValue();
}
int getDay(OffsetDateTime offsetDateTime) {
return offsetDateTime.getDayOfMonth();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.ZonedDateTime;
public class ZonedDateTimeExtractYearMonthDayIntegerValues {
int getYear(ZonedDateTime zonedDateTime) {
return zonedDateTime.getYear();
}
int getMonth(ZonedDateTime zonedDateTime) {
return zonedDateTime.getMonthValue();
}
int getDay(ZonedDateTime zonedDateTime) {
return zonedDateTime.getDayOfMonth();
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
public class DateExtractYearMonthDayIntegerValuesUnitTest {
DateExtractYearMonthDayIntegerValues extractYearMonthDateIntegerValues = new DateExtractYearMonthDayIntegerValues();
Date date;
@Before
public void setup() throws ParseException
{
date=new SimpleDateFormat("dd-MM-yyyy").parse("01-03-2018");
}
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=extractYearMonthDateIntegerValues.getYear(date);
assertThat(actualYear,is(2018));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=extractYearMonthDateIntegerValues.getMonth(date);
assertThat(actualMonth,is(02));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=extractYearMonthDateIntegerValues.getDay(date);
assertThat(actualDayOfMonth,is(01));
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.time.LocalDate;
import org.junit.Test;
public class LocalDateExtractYearMonthDayIntegerValuesUnitTest {
LocalDateExtractYearMonthDayIntegerValues localDateExtractYearMonthDayIntegerValues=new LocalDateExtractYearMonthDayIntegerValues();
LocalDate localDate=LocalDate.parse("2007-12-03");
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=localDateExtractYearMonthDayIntegerValues.getYear(localDate);
assertThat(actualYear,is(2007));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=localDateExtractYearMonthDayIntegerValues.getMonth(localDate);
assertThat(actualMonth,is(12));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=localDateExtractYearMonthDayIntegerValues.getDay(localDate);
assertThat(actualDayOfMonth,is(03));
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.time.LocalDateTime;
import org.junit.Test;
public class LocalDateTimeExtractYearMonthDayIntegerValuesUnitTest {
LocalDateTimeExtractYearMonthDayIntegerValues localDateTimeExtractYearMonthDayIntegerValues = new LocalDateTimeExtractYearMonthDayIntegerValues();
LocalDateTime localDateTime=LocalDateTime.parse("2007-12-03T10:15:30");
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=localDateTimeExtractYearMonthDayIntegerValues.getYear(localDateTime);
assertThat(actualYear,is(2007));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=localDateTimeExtractYearMonthDayIntegerValues.getMonth(localDateTime);
assertThat(actualMonth,is(12));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=localDateTimeExtractYearMonthDayIntegerValues.getDay(localDateTime);
assertThat(actualDayOfMonth,is(03));
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.time.OffsetDateTime;
import org.junit.Test;
public class OffsetDateTimeExtractYearMonthDayIntegerValuesUnitTest {
OffsetDateTimeExtractYearMonthDayIntegerValues offsetDateTimeExtractYearMonthDayIntegerValues = new OffsetDateTimeExtractYearMonthDayIntegerValues();
OffsetDateTime offsetDateTime=OffsetDateTime.parse("2007-12-03T10:15:30+01:00");
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=offsetDateTimeExtractYearMonthDayIntegerValues.getYear(offsetDateTime);
assertThat(actualYear,is(2007));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getMonth(offsetDateTime);
assertThat(actualMonth,is(12));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getDay(offsetDateTime);
assertThat(actualDayOfMonth,is(03));
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.time.ZonedDateTime;
import org.junit.Test;
public class ZonedDateTimeExtractYearMonthDayIntegerValuesUnitTest {
ZonedDateTimeExtractYearMonthDayIntegerValues zonedDateTimeExtractYearMonthDayIntegerValues = new ZonedDateTimeExtractYearMonthDayIntegerValues();
ZonedDateTime zonedDateTime=ZonedDateTime.parse("2007-12-03T10:15:30+01:00");
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=zonedDateTimeExtractYearMonthDayIntegerValues.getYear(zonedDateTime);
assertThat(actualYear,is(2007));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getMonth(zonedDateTime);
assertThat(actualMonth,is(12));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getDay(zonedDateTime);
assertThat(actualDayOfMonth,is(03));
}
}