[BAEL-8456] - Moved more articles into 'java-dates' module
This commit is contained in:
@@ -13,4 +13,12 @@
|
||||
- [Get the Current Date, Time and Timestamp in Java 8](http://www.baeldung.com/current-date-time-and-timestamp-in-java-8)
|
||||
- [Get Date Without Time in Java](http://www.baeldung.com/java-date-without-time)
|
||||
- [How to Get All Dates Between Two Dates?](http://www.baeldung.com/java-between-dates)
|
||||
- [Convert Date to LocalDate or LocalDateTime and Back](http://www.baeldung.com/java-date-to-localdate-and-localdatetime)
|
||||
- [Convert Date to LocalDate or LocalDateTime and Back](http://www.baeldung.com/java-date-to-localdate-and-localdatetime)
|
||||
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
|
||||
- [Extracting Year, Month and Day from Date in Java](http://www.baeldung.com/java-year-month-day)
|
||||
- [Guide to java.util.GregorianCalendar](http://www.baeldung.com/java-gregorian-calendar)
|
||||
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
|
||||
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
|
||||
- [Calculate Age in Java](http://www.baeldung.com/java-get-age)
|
||||
- [Increment Date in Java](http://www.baeldung.com/java-increment-date)
|
||||
- [Add Hours To a Date In Java](http://www.baeldung.com/java-add-hours-date)
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.datetime.modify;
|
||||
|
||||
import org.apache.commons.lang3.time.DateUtils;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class DateIncrementer {
|
||||
private static final Logger log = Logger.getLogger(DateIncrementer.class.getName());
|
||||
private static final int INCREMENT_BY_IN_DAYS = 1;
|
||||
|
||||
public static String addOneDay(String date) {
|
||||
return LocalDate
|
||||
.parse(date)
|
||||
.plusDays(INCREMENT_BY_IN_DAYS)
|
||||
.toString();
|
||||
}
|
||||
|
||||
public static String addOneDayJodaTime(String date) {
|
||||
DateTime dateTime = new DateTime(date);
|
||||
return dateTime
|
||||
.plusDays(INCREMENT_BY_IN_DAYS)
|
||||
.toString("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
public static String addOneDayCalendar(String date) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.setTime(sdf.parse(date));
|
||||
c.add(Calendar.DATE, 1);
|
||||
return sdf.format(c.getTime());
|
||||
}
|
||||
|
||||
public static String addOneDayApacheCommons(String date) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date incrementedDate = DateUtils.addDays(sdf.parse(date), 1);
|
||||
return sdf.format(incrementedDate);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ParseException {
|
||||
String date = LocalDate
|
||||
.now()
|
||||
.toString();
|
||||
log.info("Current date = " + date);
|
||||
|
||||
String incrementedDateJava8 = DateIncrementer.addOneDay(date);
|
||||
log.info("Date incremented by one day using (Java 8): " + incrementedDateJava8);
|
||||
|
||||
String incrementedDateJodaTime = DateIncrementer.addOneDayJodaTime(date);
|
||||
log.info("Date incremented by one day using (Joda-Time): " + incrementedDateJodaTime);
|
||||
|
||||
String incrementedDateCalendar = addOneDayCalendar(date);
|
||||
log.info("Date incremented by one day using (java.util.Calendar): " + incrementedDateCalendar);
|
||||
|
||||
String incrementedDateApacheCommons = addOneDayApacheCommons(date);
|
||||
log.info("Date incremented by one day using (Apache Commons DateUtils): " + incrementedDateApacheCommons);
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TimezoneDisplay {
|
||||
|
||||
public enum OffsetBase {
|
||||
GMT, UTC
|
||||
}
|
||||
|
||||
public List<String> getTimeZoneList(OffsetBase base) {
|
||||
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return availableZoneIds
|
||||
.stream()
|
||||
.map(ZoneId::of)
|
||||
.sorted(new ZoneComparator())
|
||||
.map(id -> String.format("(%s%s) %s", base, getOffset(now, id), id.getId()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private String getOffset(LocalDateTime dateTime, ZoneId id) {
|
||||
return dateTime
|
||||
.atZone(id)
|
||||
.getOffset()
|
||||
.getId()
|
||||
.replace("Z", "+00:00");
|
||||
}
|
||||
|
||||
private class ZoneComparator implements Comparator<ZoneId> {
|
||||
|
||||
@Override
|
||||
public int compare(ZoneId zoneId1, ZoneId zoneId2) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
ZoneOffset offset1 = now
|
||||
.atZone(zoneId1)
|
||||
.getOffset();
|
||||
|
||||
ZoneOffset offset2 = now
|
||||
.atZone(zoneId2)
|
||||
.getOffset();
|
||||
|
||||
return offset1.compareTo(offset2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TimezoneDisplayApp {
|
||||
|
||||
public static void main(String... args) {
|
||||
TimezoneDisplay display = new TimezoneDisplay();
|
||||
|
||||
System.out.println("Time zones in UTC:");
|
||||
List<String> utc = display.getTimeZoneList(TimezoneDisplay.OffsetBase.UTC);
|
||||
utc.forEach(System.out::println);
|
||||
|
||||
System.out.println("Time zones in GMT:");
|
||||
List<String> gmt = display.getTimeZoneList(TimezoneDisplay.OffsetBase.GMT);
|
||||
gmt.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TimezoneDisplayJava7 {
|
||||
|
||||
public enum OffsetBase {
|
||||
GMT, UTC
|
||||
}
|
||||
|
||||
public List<String> getTimeZoneList(TimezoneDisplayJava7.OffsetBase base) {
|
||||
String[] availableZoneIds = TimeZone.getAvailableIDs();
|
||||
List<String> result = new ArrayList<>(availableZoneIds.length);
|
||||
|
||||
for (String zoneId : availableZoneIds) {
|
||||
TimeZone curTimeZone = TimeZone.getTimeZone(zoneId);
|
||||
|
||||
String offset = calculateOffset(curTimeZone.getRawOffset());
|
||||
|
||||
result.add(String.format("(%s%s) %s", base, offset, zoneId));
|
||||
}
|
||||
|
||||
Collections.sort(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private String calculateOffset(int rawOffset) {
|
||||
if (rawOffset == 0) {
|
||||
return "+00:00";
|
||||
}
|
||||
long hours = TimeUnit.MILLISECONDS.toHours(rawOffset);
|
||||
long minutes = TimeUnit.MILLISECONDS.toMinutes(rawOffset);
|
||||
minutes = Math.abs(minutes - TimeUnit.HOURS.toMinutes(hours));
|
||||
|
||||
return String.format("%+03d:%02d", hours, Math.abs(minutes));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TimezoneDisplayJava7App {
|
||||
|
||||
public static void main(String... args) {
|
||||
TimezoneDisplayJava7 display = new TimezoneDisplayJava7();
|
||||
|
||||
System.out.println("Time zones in UTC:");
|
||||
List<String> utc = display.getTimeZoneList(TimezoneDisplayJava7.OffsetBase.UTC);
|
||||
for (String timeZone : utc) {
|
||||
System.out.println(timeZone);
|
||||
}
|
||||
|
||||
System.out.println("Time zones in GMT:");
|
||||
List<String> gmt = display.getTimeZoneList(TimezoneDisplayJava7.OffsetBase.GMT);
|
||||
for (String timeZone : gmt) {
|
||||
System.out.println(timeZone);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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() );
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user