Merge branch 'master' into BAEL-16646-2

This commit is contained in:
Alessio Stalla
2019-10-24 13:20:08 +02:00
138 changed files with 3689 additions and 290 deletions

View File

@@ -0,0 +1,21 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
public class UseDateTimeFormatter {
public String formatAsIsoDate(LocalDateTime localDateTime) {
return localDateTime.format(DateTimeFormatter.ISO_DATE);
}
public String formatCustom(LocalDateTime localDateTime, String pattern) {
return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
}
public String formatWithStyleAndLocale(LocalDateTime localDateTime, FormatStyle formatStyle, Locale locale) {
return localDateTime.format(DateTimeFormatter.ofLocalizedDateTime(formatStyle)
.withLocale(locale));
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class UseOffsetDateTime {
public OffsetDateTime offsetOfLocalDateTimeAndOffset(LocalDateTime localDateTime, ZoneOffset offset) {
return OffsetDateTime.of(localDateTime, offset);
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.FormatStyle;
import java.util.Locale;
import org.junit.Test;
public class UseDateTimeFormatterUnitTest {
private final UseDateTimeFormatter subject = new UseDateTimeFormatter();
private final LocalDateTime localDateTime = LocalDateTime.of(2015, Month.JANUARY, 25, 6, 30);
@Test
public void givenALocalDate_whenFormattingAsIso_thenPass() {
String result = subject.formatAsIsoDate(localDateTime);
assertThat(result).isEqualTo("2015-01-25");
}
@Test
public void givenALocalDate_whenFormattingWithPattern_thenPass() {
String result = subject.formatCustom(localDateTime, "yyyy/MM/dd");
assertThat(result).isEqualTo("2015/01/25");
}
@Test
public void givenALocalDate_whenFormattingWithStyleAndLocale_thenPass() {
String result = subject.formatWithStyleAndLocale(localDateTime, FormatStyle.MEDIUM, Locale.UK);
assertThat(result).isEqualTo("25 Jan 2015, 06:30:00");
}
}

View File

@@ -0,0 +1,24 @@
package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import org.junit.Test;
public class UseOffsetDateTimeUnitTest {
private final UseOffsetDateTime subject = new UseOffsetDateTime();
@Test
public void givenAZoneOffSetAndLocalDateTime_whenCombing_thenValidResult() {
ZoneOffset offset = ZoneOffset.of("+02:00");
LocalDateTime localDateTime = LocalDateTime.of(2015, Month.FEBRUARY, 20, 6, 30);
OffsetDateTime result = subject.offsetOfLocalDateTimeAndOffset(localDateTime, offset);
assertThat(result.toString()).isEqualTo("2015-02-20T06:30+02:00");
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.junit.Test;
public class UseToInstantUnitTest {
private UseToInstant subject = new UseToInstant();
@Test
public void givenAGregorianCalenderDate_whenConvertingToLocalDate_thenAsExpected() {
GregorianCalendar givenCalender = new GregorianCalendar(2018, Calendar.JULY, 28);
LocalDateTime localDateTime = subject.convertDateToLocalDate(givenCalender);
assertThat(localDateTime).isEqualTo("2018-07-28T00:00:00");
}
@Test
public void givenADate_whenConvertingToLocalDate_thenAsExpected() {
Date givenDate = new Date(1465817690000L);
LocalDateTime localDateTime = subject.convertDateToLocalDate(givenDate);
assertThat(localDateTime).isEqualTo("2016-06-13T13:34:50");
}
}