Add additional AssertJ examples

This commit is contained in:
Grzegorz Piwowarek 2016-06-25 17:23:52 +02:00
parent 96182a2bf2
commit a661e0e3c9
5 changed files with 97 additions and 124 deletions

View File

@ -63,6 +63,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>

View File

@ -1,54 +0,0 @@
package com.baeldung.date_migration;
import java.text.ParseException;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class NewApi {
public void currentTime() {
ZonedDateTime now = ZonedDateTime.now();
}
public void specificTime() {
LocalDate birthDay = LocalDate.of(1990, Month.DECEMBER, 15);
}
public void extractMonth() {
Month month = LocalDateTime.now().getMonth();
}
public void subtractTime() {
LocalDateTime fiveHoursBefore = LocalDateTime.now().minusHours(5);
}
public void alterField() {
LocalDateTime inJune = LocalDateTime.now().withMonth(Month.JUNE.getValue());
}
public void truncate() {
LocalTime truncated = LocalTime.now().truncatedTo(ChronoUnit.HOURS);
}
public void convertTimeZone() {
ZonedDateTime centralEastern = LocalDateTime.now().atZone(ZoneId.of("CET"));
}
public void getTimeSpan() {
LocalDateTime now = LocalDateTime.now();
LocalDateTime hourLater = LocalDateTime.now().plusHours(1);
Duration span = Duration.between(now, hourLater);
}
public void formatAndParse() throws ParseException {
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = now.format(formatter);
LocalDate parsedDate = LocalDate.parse(formattedDate, formatter);
}
public void daysInMonth() {
int daysInMonth = YearMonth.of(1990, 2).lengthOfMonth();
}
}

View File

@ -1,68 +0,0 @@
package com.baeldung.date_migration;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class OldApi {
public void currentTime() {
Date now = new Date();
}
public void specificTime () {
Date birthDay = new GregorianCalendar(1990, Calendar.DECEMBER, 15).getTime();
}
public void extractMonth() {
int month = new GregorianCalendar().get(Calendar.MONTH);
}
public void subtractTime() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.add(Calendar.HOUR_OF_DAY, -5);
Date fiveHoursBefore = calendar.getTime();
}
public void alterField() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.MONTH, Calendar.JUNE);
Date inJune = calendar.getTime();
}
public void truncate() {
Calendar now = Calendar.getInstance();
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
Date truncated = now.getTime();
}
public void convertTimeZone() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("CET"));
Date centralEastern = calendar.getTime();
}
public void getTimeSpan() {
GregorianCalendar calendar = new GregorianCalendar();
Date now = new Date();
calendar.add(Calendar.HOUR, 1);
Date hourLater = calendar.getTime();
long elapsed = hourLater.getTime() - now.getTime();
}
public void formatAndParse() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
String formattedDate = dateFormat.format(now);
Date parsedDate = dateFormat.parse(formattedDate);
}
public void daysInMonth() {
Calendar calendar = new GregorianCalendar(1990, Calendar.FEBRUARY, 20);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.date_migration;
package com.baeldung.dateapi;
import java.time.Instant;
import java.time.ZoneId;
@ -7,7 +7,7 @@ import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Conversion {
public class ConversionExample {
public static void main(String[] args) {
Instant instantFromCalendar = GregorianCalendar.getInstance().toInstant();
ZonedDateTime zonedDateTimeFromCalendar = new GregorianCalendar().toZonedDateTime();

View File

@ -0,0 +1,89 @@
package com.baeldung.dateapi;
import org.junit.Test;
import java.text.ParseException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import static org.assertj.core.api.Assertions.assertThat;
public class JavaUtilTimeTest {
@Test
public void currentTime() {
final LocalDate now = LocalDate.now();
System.out.println(now);
// there is not much to test here
}
@Test
public void specificTime() {
LocalDate birthDay = LocalDate.of(1990, Month.DECEMBER, 15);
System.out.println(birthDay);
// there is not much to test here
}
@Test
public void extractMonth() {
Month month = LocalDate.of(1990, Month.DECEMBER, 15).getMonth();
assertThat(month).isEqualTo(Month.DECEMBER);
}
@Test
public void subtractTime() {
LocalDateTime fiveHoursBefore = LocalDateTime.of(1990, Month.DECEMBER, 15, 15, 0).minusHours(5);
assertThat(fiveHoursBefore.getHour()).isEqualTo(10);
}
@Test
public void alterField() {
LocalDateTime inJune = LocalDateTime.of(1990, Month.DECEMBER, 15, 15, 0).with(Month.JUNE);
assertThat(inJune.getMonth()).isEqualTo(Month.JUNE);
}
@Test
public void truncate() {
LocalTime truncated = LocalTime.of(15, 12, 34).truncatedTo(ChronoUnit.HOURS);
assertThat(truncated).isEqualTo(LocalTime.of(15, 0, 0));
}
@Test
public void getTimeSpan() {
LocalDateTime now = LocalDateTime.now();
LocalDateTime hourLater = now.plusHours(1);
Duration span = Duration.between(now, hourLater);
assertThat(span).isEqualTo(Duration.ofHours(1));
}
@Test
public void formatAndParse() throws ParseException {
LocalDate someDate = LocalDate.of(2016, 12, 7);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = someDate.format(formatter);
LocalDate parsedDate = LocalDate.parse(formattedDate, formatter);
assertThat(formattedDate).isEqualTo("2016-12-07");
assertThat(parsedDate).isEqualTo(someDate);
}
@Test
public void daysInMonth() {
int daysInMonth = YearMonth.of(1990, 2).lengthOfMonth();
assertThat(daysInMonth).isEqualTo(28);
}
}