Merge pull request #4579 from amitabhmandal/BAEL-1896

BAEL-1896: Guide to GregorianCalendar
This commit is contained in:
Tom Hombergs 2018-07-08 20:41:35 +02:00 committed by GitHub
commit 455385d66e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 297 additions and 0 deletions

View File

@ -0,0 +1,69 @@
package com.baeldung.gregorian.calendar;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class GregorianCalendarExample {
public Date setMonth(GregorianCalendar calendar, int amount) {
calendar.set(Calendar.MONTH, amount);
return calendar.getTime();
}
public Date rollAdd(GregorianCalendar calendar, int amount) {
calendar.roll(GregorianCalendar.MONTH, amount);
return calendar.getTime();
}
public boolean isLeapYearExample(int year) {
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
return cal.isLeapYear(year);
}
public Date subtractDays(GregorianCalendar calendar, int daysToSubtract) {
GregorianCalendar cal = calendar;
cal.add(Calendar.DATE, -daysToSubtract);
return cal.getTime();
}
public Date addDays(GregorianCalendar calendar, int daysToAdd) {
GregorianCalendar cal = calendar;
cal.add(Calendar.DATE, daysToAdd);
return cal.getTime();
}
public XMLGregorianCalendar toXMLGregorianCalendar(GregorianCalendar calendar) throws DatatypeConfigurationException {
DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
return datatypeFactory.newXMLGregorianCalendar(calendar);
}
public Date toDate(XMLGregorianCalendar calendar) {
return calendar.toGregorianCalendar()
.getTime();
}
public int compareDates(GregorianCalendar firstDate, GregorianCalendar secondDate) {
return firstDate.compareTo(secondDate);
}
public String formatDate(GregorianCalendar calendar) {
return calendar.toZonedDateTime()
.format(DateTimeFormatter.ofPattern("d MMM uuuu"));
}
}

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