[BAEL-8456] - Moved more articles into 'java-dates' module

This commit is contained in:
amit2103
2018-08-26 00:49:30 +05:30
parent 3bd1ed4ece
commit c5e1d6f0ef
23 changed files with 10 additions and 11 deletions

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));
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.datetime.modify;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DateIncrementerUnitTest {
private final static String DATE_TO_INCREMENT = "2018-07-03";
private final static String EXPECTED_DATE = "2018-07-04";
@Test
public void givenDate_whenUsingJava8_thenAddOneDay() throws Exception {
String incrementedDate = DateIncrementer.addOneDay(DATE_TO_INCREMENT);
assertEquals(EXPECTED_DATE, incrementedDate);
}
@Test
public void givenDate_whenUsingJodaTime_thenAddOneDay() throws Exception {
String incrementedDate = DateIncrementer.addOneDayJodaTime(DATE_TO_INCREMENT);
assertEquals(EXPECTED_DATE, incrementedDate);
}
@Test
public void givenDate_whenUsingCalendar_thenAddOneDay() throws Exception {
String incrementedDate = DateIncrementer.addOneDayCalendar(DATE_TO_INCREMENT);
assertEquals(EXPECTED_DATE, incrementedDate);
}
@Test
public void givenDate_whenUsingApacheCommons_thenAddOneDay() throws Exception {
String incrementedDate = DateIncrementer.addOneDayApacheCommons(DATE_TO_INCREMENT);
assertEquals(EXPECTED_DATE, incrementedDate);
}
}

View File

@@ -0,0 +1,228 @@
package com.baeldung.gregorian.calendar;
import static org.junit.Assert.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.junit.Test;
import com.baeldung.gregorian.calendar.GregorianCalendarExample;
public class GregorianCalendarTester {
@Test
public void test_Calendar_Return_Type_Valid() {
Calendar calendar = Calendar.getInstance();
assert ("gregory".equals(calendar.getCalendarType()));
}
@Test
public void test_Calendar_Return_Type_InValid() {
Calendar calendar = Calendar.getInstance();
assertNotEquals("gregorys", calendar.getCalendarType());
}
@Test(expected = ClassCastException.class)
public void test_Class_Cast_Exception() {
TimeZone tz = TimeZone.getTimeZone("GMT+9:00");
Locale loc = new Locale("ja", "JP", "JP");
Calendar calendar = Calendar.getInstance(loc);
GregorianCalendar gc = (GregorianCalendar) calendar;
}
@Test
public void test_Getting_Calendar_information() {
GregorianCalendar calendar = new GregorianCalendar(2018, 5, 28);
assertTrue(false == calendar.isLeapYear(calendar.YEAR));
assertTrue(52 == calendar.getWeeksInWeekYear());
assertTrue(2018 == calendar.getWeekYear());
assertTrue(30 == calendar.getActualMaximum(calendar.DAY_OF_MONTH));
assertTrue(1 == calendar.getActualMinimum(calendar.DAY_OF_MONTH));
assertTrue(1 == calendar.getGreatestMinimum(calendar.DAY_OF_MONTH));
assertTrue(28 == calendar.getLeastMaximum(calendar.DAY_OF_MONTH));
assertTrue(31 == calendar.getMaximum(calendar.DAY_OF_MONTH));
assertTrue(1 == calendar.getMinimum(calendar.DAY_OF_MONTH));
assertTrue(52 == calendar.getWeeksInWeekYear());
}
@Test
public void test_Compare_Date_FirstDate_Greater_SecondDate() {
GregorianCalendar firstDate = new GregorianCalendar(2018, 6, 28);
GregorianCalendar secondDate = new GregorianCalendar(2018, 5, 28);
assertTrue(1 == firstDate.compareTo(secondDate));
}
@Test
public void test_Compare_Date_FirstDate_Smaller_SecondDate() {
GregorianCalendar firstDate = new GregorianCalendar(2018, 5, 28);
GregorianCalendar secondDate = new GregorianCalendar(2018, 6, 28);
assertTrue(-1 == firstDate.compareTo(secondDate));
}
@Test
public void test_Compare_Date_Both_Dates_Equal() {
GregorianCalendar firstDate = new GregorianCalendar(2018, 6, 28);
GregorianCalendar secondDate = new GregorianCalendar(2018, 6, 28);
assertTrue(0 == firstDate.compareTo(secondDate));
}
@Test
public void test_date_format() {
GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
GregorianCalendar calendar = new GregorianCalendar(2018, 6, 28);
assertEquals("28 Jul 2018", calendarDemo.formatDate(calendar));
}
@Test
public void test_dateFormatdMMMuuuu() {
String expectedDate = new GregorianCalendar(2018, 6, 28).toZonedDateTime()
.format(DateTimeFormatter.ofPattern("d MMM uuuu"));
assertEquals("28 Jul 2018", expectedDate);
}
@Test
public void test_addDays() {
GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
GregorianCalendar calendarActual = new GregorianCalendar(2018, 6, 28);
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
calendarExpected.add(Calendar.DATE, 1);
Date expectedDate = calendarExpected.getTime();
assertEquals(expectedDate, calendarDemo.addDays(calendarActual, 1));
}
@Test
public void test_whenAddOneDay_thenMonthIsChanged() {
final int finalDay1 = 1;
final int finalMonthJul = 6;
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 5, 30);
calendarExpected.add(Calendar.DATE, 1);
System.out.println(calendarExpected.getTime());
assertEquals(calendarExpected.get(Calendar.DATE), finalDay1);
assertEquals(calendarExpected.get(Calendar.MONTH), finalMonthJul);
}
@Test
public void test_whenSubtractOneDay_thenMonthIsChanged() {
final int finalDay31 = 31;
final int finalMonthMay = 4;
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 5, 1);
calendarExpected.add(Calendar.DATE, -1);
assertEquals(calendarExpected.get(Calendar.DATE), finalDay31);
assertEquals(calendarExpected.get(Calendar.MONTH), finalMonthMay);
}
@Test
public void test_subDays() {
GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
GregorianCalendar calendarActual = new GregorianCalendar(2018, 6, 28);
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
calendarExpected.add(Calendar.DATE, -1);
Date expectedDate = calendarExpected.getTime();
assertEquals(expectedDate, calendarDemo.subtractDays(calendarActual, 1));
}
@Test
public void test_rollAdd() {
GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
GregorianCalendar calendarActual = new GregorianCalendar(2018, 6, 28);
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
calendarExpected.roll(Calendar.MONTH, 8);
Date expectedDate = calendarExpected.getTime();
assertEquals(expectedDate, calendarDemo.rollAdd(calendarActual, 8));
}
@Test
public void test_whenRollUpOneMonth_thenYearIsUnchanged() {
final int rolledUpMonthJuly = 7, orginalYear2018 = 2018;
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
calendarExpected.roll(Calendar.MONTH, 1);
assertEquals(calendarExpected.get(Calendar.MONTH), rolledUpMonthJuly);
assertEquals(calendarExpected.get(Calendar.YEAR), orginalYear2018);
}
@Test
public void test_whenRollDownOneMonth_thenYearIsUnchanged() {
final int rolledDownMonthJune = 5, orginalYear2018 = 2018;
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
calendarExpected.roll(Calendar.MONTH, -1);
assertEquals(calendarExpected.get(Calendar.MONTH), rolledDownMonthJune);
assertEquals(calendarExpected.get(Calendar.YEAR), orginalYear2018);
}
@Test
public void test_rollSubtract() {
GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
GregorianCalendar calendarActual = new GregorianCalendar(2018, 6, 28);
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
calendarExpected.roll(Calendar.MONTH, -8);
Date expectedDate = calendarExpected.getTime();
assertEquals(expectedDate, calendarDemo.rollAdd(calendarActual, -8));
}
@Test
public void test_setMonth() {
GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
GregorianCalendar calendarActual = new GregorianCalendar(2018, 6, 28);
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
calendarExpected.set(Calendar.MONTH, 3);
Date expectedDate = calendarExpected.getTime();
assertEquals(expectedDate, calendarDemo.setMonth(calendarActual, 3));
}
@Test
public void test_setMonthApril() {
final int setMonthApril = 3, orginalYear2018 = 2018, originalDate28 = 28;
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
calendarExpected.set(Calendar.MONTH, 3);
assertEquals(calendarExpected.get(Calendar.MONTH), setMonthApril);
assertEquals(calendarExpected.get(Calendar.YEAR), orginalYear2018);
assertEquals(calendarExpected.get(Calendar.DATE), originalDate28);
}
@Test
public void test_toXMLGregorianCalendar() throws DatatypeConfigurationException {
GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
GregorianCalendar calendarActual = new GregorianCalendar(2018, 6, 28);
GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
XMLGregorianCalendar expectedXMLGregorianCalendar = datatypeFactory
.newXMLGregorianCalendar(calendarExpected);
assertEquals(expectedXMLGregorianCalendar,
calendarDemo.toXMLGregorianCalendar(calendarActual));
}
@Test
public void test_isLeapYear_True() {
GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
assertEquals(true, calendarDemo.isLeapYearExample(2016));
}
@Test
public void test_isLeapYear_False() {
GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
assertEquals(false, calendarDemo.isLeapYearExample(2018));
}
@Test
public void test_toDate() throws DatatypeConfigurationException {
GregorianCalendar calendarActual = new GregorianCalendar(2018, 6, 28);
DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
XMLGregorianCalendar expectedXMLGregorianCalendar = datatypeFactory
.newXMLGregorianCalendar(calendarActual);
expectedXMLGregorianCalendar.toGregorianCalendar().getTime();
assertEquals(calendarActual.getTime(),
expectedXMLGregorianCalendar.toGregorianCalendar().getTime() );
}
}

View File

@@ -18,7 +18,7 @@ public class TimeApiUnitTest {
Date endDate = endCalendar.getTime();
List<Date> dates = TimeApi.getDatesBetweenUsingJava7(startDate, endDate);
assertEquals(dates.size(), 3);
assertEquals(dates.size(), 2);
Calendar calendar = Calendar.getInstance();
Date date1 = calendar.getTime();

View File

@@ -0,0 +1,76 @@
package com.baeldung.time;
import static org.junit.Assert.assertEquals;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.time.StopWatch;
import org.junit.Test;
public class ElapsedTimeUnitTest {
@Test
public void givenRunningTask_whenMeasuringTimeWithCurrentTimeMillis_thenGetElapsedTime() throws InterruptedException {
long start = System.currentTimeMillis();
simulateRunningTask();
long finish = System.currentTimeMillis();
long timeElapsed = finish - start;
assertEquals(true, (2000L <= timeElapsed) && (timeElapsed <= 3000L));
}
@Test
public void giveRunningTask_whenMeasuringTimeWithNanoTime_thenGetElapsedTime() throws InterruptedException {
long start = System.nanoTime();
simulateRunningTask();
long finish = System.nanoTime();
long timeElapsed = finish - start;
assertEquals(true, (2000000000L <= timeElapsed) && (timeElapsed <= 3000000000L));
}
@Test
public void givenRunningTask_whenMeasuringTimeWithStopWatch_thenGetElapsedTime() throws InterruptedException {
StopWatch watch = new StopWatch();
watch.start();
simulateRunningTask();
watch.stop();
long timeElapsed = watch.getTime();
assertEquals(true, (2000L <= timeElapsed) && (timeElapsed <= 3000L));
}
@Test
public void givenRunningTask_whenMeasuringTimeWithInstantClass_thenGetElapsedTime() throws InterruptedException {
Instant start = Instant.now();
simulateRunningTask();
Instant finish = Instant.now();
long timeElapsed = Duration.between(start, finish).toMillis();
assertEquals(true, (2000L <= timeElapsed) && (timeElapsed <= 3000L));
}
/**
* Simulate task running for 2.5 seconds.
*
* @throws InterruptedException
*/
private static void simulateRunningTask() throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(2500); // 2.5 seconds
}
}