Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -14,3 +14,4 @@
|
|||||||
- [Bubble Sort in Java](http://www.baeldung.com/java-bubble-sort)
|
- [Bubble Sort in Java](http://www.baeldung.com/java-bubble-sort)
|
||||||
- [Introduction to JGraphT](http://www.baeldung.com/jgrapht)
|
- [Introduction to JGraphT](http://www.baeldung.com/jgrapht)
|
||||||
- [Introduction to Minimax Algorithm](http://www.baeldung.com/java-minimax-algorithm)
|
- [Introduction to Minimax Algorithm](http://www.baeldung.com/java-minimax-algorithm)
|
||||||
|
- [How to Calculate Levenshtein Distance in Java?](http://www.baeldung.com/java-levenshtein-distance)
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
### Relevant articles
|
||||||
|
|
||||||
|
- [Introduction to Apache Spark](http://www.baeldung.com/apache-spark)
|
||||||
@@ -65,6 +65,24 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<forkCount>3</forkCount>
|
||||||
|
<reuseForks>true</reuseForks>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||||
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
|
<exclude>**/JdbcTest.java</exclude>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
@@ -107,4 +125,5 @@
|
|||||||
</pluginRepositories>
|
</pluginRepositories>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class CasSecuredAppApplicationTests {
|
public class CasSecuredAppApplicationIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void contextLoads() {
|
public void contextLoads() {
|
||||||
@@ -109,7 +109,7 @@ cas.authn.jdbc.query[0].sql=SELECT * FROM users WHERE email = ?
|
|||||||
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
|
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
|
||||||
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
|
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
|
||||||
cas.authn.jdbc.query[0].user=root
|
cas.authn.jdbc.query[0].user=root
|
||||||
cas.authn.jdbc.query[0].password=
|
cas.authn.jdbc.query[0].password=root
|
||||||
cas.authn.jdbc.query[0].ddlAuto=none
|
cas.authn.jdbc.query[0].ddlAuto=none
|
||||||
#cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
|
#cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
|
||||||
cas.authn.jdbc.query[0].driverClass=com.mysql.cj.jdbc.Driver
|
cas.authn.jdbc.query[0].driverClass=com.mysql.cj.jdbc.Driver
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
-- Dumping database structure for test
|
||||||
|
CREATE DATABASE IF NOT EXISTS `test` /*!40100 DEFAULT CHARACTER SET latin1 */;
|
||||||
|
USE `test`;
|
||||||
|
|
||||||
|
-- Dumping structure for table test.users
|
||||||
|
CREATE TABLE IF NOT EXISTS `users` (
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`email` varchar(50) DEFAULT NULL,
|
||||||
|
`password` text DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
|
||||||
|
|
||||||
|
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
|
||||||
|
INSERT INTO `users` (`id`, `email`, `password`) VALUES
|
||||||
|
(1, '[email protected]', 'Mellon');
|
||||||
|
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
|
||||||
@@ -30,3 +30,5 @@
|
|||||||
- [The Difference Between map() and flatMap()](http://www.baeldung.com/java-difference-map-and-flatmap)
|
- [The Difference Between map() and flatMap()](http://www.baeldung.com/java-difference-map-and-flatmap)
|
||||||
- [Merging Streams in Java](http://www.baeldung.com/java-merge-streams)
|
- [Merging Streams in Java](http://www.baeldung.com/java-merge-streams)
|
||||||
- [“Stream has already been operated upon or closed” Exception in Java](http://www.baeldung.com/java-stream-operated-upon-or-closed-exception)
|
- [“Stream has already been operated upon or closed” Exception in Java](http://www.baeldung.com/java-stream-operated-upon-or-closed-exception)
|
||||||
|
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
|
||||||
|
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
class ClassWithAnnotation {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
class ClassWithDeprecatedMethod {
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
static void deprecatedMethod() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
class ClassWithSafeVarargs<T> {
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
final void iterateOverVarargs(T... args) {
|
||||||
|
for (T x : args) {
|
||||||
|
// do stuff with x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
class ClassWithSuppressWarnings {
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
void useDeprecatedMethod() {
|
||||||
|
ClassWithDeprecatedMethod.deprecatedMethod(); // no warning is generated here
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
interface IntConsumer {
|
||||||
|
|
||||||
|
void accept(Integer number);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.Repeatable;
|
||||||
|
|
||||||
|
@Repeatable(Intervals.class)
|
||||||
|
@interface Interval {
|
||||||
|
int hour() default 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
public class IntervalUsage {
|
||||||
|
|
||||||
|
@Interval(hour = 17)
|
||||||
|
@Interval(hour = 13)
|
||||||
|
void doPeriodicCleanup() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
@interface Intervals {
|
||||||
|
Interval[] value();
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Inherited
|
||||||
|
@Documented
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.LOCAL_VARIABLE, ElementType.FIELD})
|
||||||
|
@interface MyAnnotation {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
class MyAnnotationTarget {
|
||||||
|
|
||||||
|
// this is OK
|
||||||
|
@MyAnnotation
|
||||||
|
String someField;
|
||||||
|
|
||||||
|
// @MyAnnotation <- this is invalid usage!
|
||||||
|
void doSomething() {
|
||||||
|
|
||||||
|
// this also works
|
||||||
|
@MyAnnotation
|
||||||
|
String localVariable;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
interface MyOperation {
|
||||||
|
|
||||||
|
void perform();
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.annotations;
|
||||||
|
|
||||||
|
class MyOperationImpl implements MyOperation {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void perform() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.datetime;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class which shows different ways of converting java.util.Date into java.time.LocalDate.
|
||||||
|
*
|
||||||
|
* @author abialas
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DateToLocalDateConverter {
|
||||||
|
|
||||||
|
public static LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
|
||||||
|
return dateToConvert.toInstant()
|
||||||
|
.atZone(ZoneId.systemDefault())
|
||||||
|
.toLocalDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) {
|
||||||
|
return new java.sql.Date(dateToConvert.getTime()).toLocalDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) {
|
||||||
|
return Instant.ofEpochMilli(dateToConvert.getTime())
|
||||||
|
.atZone(ZoneId.systemDefault())
|
||||||
|
.toLocalDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.datetime;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class which shows different ways of converting java.util.Date into java.time.LocalDateTime.
|
||||||
|
*
|
||||||
|
* @author abialas
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DateToLocalDateTimeConverter {
|
||||||
|
|
||||||
|
public static LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
|
||||||
|
return dateToConvert.toInstant()
|
||||||
|
.atZone(ZoneId.systemDefault())
|
||||||
|
.toLocalDateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert) {
|
||||||
|
return new java.sql.Timestamp(dateToConvert.getTime()).toLocalDateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) {
|
||||||
|
return Instant.ofEpochMilli(dateToConvert.getTime())
|
||||||
|
.atZone(ZoneId.systemDefault())
|
||||||
|
.toLocalDateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.datetime;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class which shows different ways of converting java.time.LocalDateTime into java.util.Date.
|
||||||
|
*
|
||||||
|
* @author abialas
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LocalDateTimeToDateConverter {
|
||||||
|
|
||||||
|
public static Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
|
||||||
|
return java.sql.Timestamp.valueOf(dateToConvert);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date convertToDateViaInstant(LocalDateTime dateToConvert) {
|
||||||
|
return java.util.Date.from(dateToConvert.atZone(ZoneId.systemDefault())
|
||||||
|
.toInstant());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.datetime;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class which shows different ways of converting java.time.LocalDate into java.util.Date.
|
||||||
|
*
|
||||||
|
* @author abialas
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LocalDateToDateConverter {
|
||||||
|
|
||||||
|
public static Date convertToDateViaSqlDate(LocalDate dateToConvert) {
|
||||||
|
return java.sql.Date.valueOf(dateToConvert);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date convertToDateViaInstant(LocalDate dateToConvert) {
|
||||||
|
return java.util.Date.from(dateToConvert.atStartOfDay()
|
||||||
|
.atZone(ZoneId.systemDefault())
|
||||||
|
.toInstant());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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,72 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.datetime;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.temporal.ChronoField;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* JUnits for {@link DateToLocalDateConverter} class.
|
||||||
|
*
|
||||||
|
* @author abialas
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DateToLocalDateConverterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010WhenConvertViaInstant() {
|
||||||
|
// given
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(2010, 10, 10);
|
||||||
|
Date dateToConvert = calendar.getTime();
|
||||||
|
|
||||||
|
// when
|
||||||
|
LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaInstant(dateToConvert);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, localDate.get(ChronoField.YEAR));
|
||||||
|
assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR));
|
||||||
|
assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010WhenConvertViaMiliseconds() {
|
||||||
|
// given
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(2010, 10, 10);
|
||||||
|
Date dateToConvert = calendar.getTime();
|
||||||
|
|
||||||
|
// when
|
||||||
|
LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaMilisecond(dateToConvert);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, localDate.get(ChronoField.YEAR));
|
||||||
|
assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR));
|
||||||
|
assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010WhenConvertViaSqlDate() {
|
||||||
|
// given
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(2010, 10, 10);
|
||||||
|
Date dateToConvert = calendar.getTime();
|
||||||
|
|
||||||
|
// when
|
||||||
|
LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaSqlDate(dateToConvert);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, localDate.get(ChronoField.YEAR));
|
||||||
|
assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR));
|
||||||
|
assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.datetime;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.temporal.ChronoField;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* JUnits for {@link DateToLocalDateTimeConverter} class.
|
||||||
|
*
|
||||||
|
* @author abialas
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DateToLocalDateTimeConverterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() {
|
||||||
|
// given
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(2010, 10, 10, 8, 20);
|
||||||
|
Date dateToConvert = calendar.getTime();
|
||||||
|
|
||||||
|
// when
|
||||||
|
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaInstant(dateToConvert);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||||
|
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||||
|
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||||
|
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||||
|
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaMiliseconds() {
|
||||||
|
// given
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(2010, 10, 10, 8, 20);
|
||||||
|
Date dateToConvert = calendar.getTime();
|
||||||
|
|
||||||
|
// when
|
||||||
|
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaMilisecond(dateToConvert);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||||
|
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||||
|
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||||
|
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||||
|
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaSqlTimestamp() {
|
||||||
|
// given
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(2010, 10, 10, 8, 20);
|
||||||
|
Date dateToConvert = calendar.getTime();
|
||||||
|
|
||||||
|
// when
|
||||||
|
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaSqlTimestamp(dateToConvert);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||||
|
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||||
|
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||||
|
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||||
|
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.datetime;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* JUnits for {@link LocalDateTimeToDateConverter} class.
|
||||||
|
*
|
||||||
|
* @author abialas
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LocalDateTimeToDateConverterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() {
|
||||||
|
// given
|
||||||
|
LocalDateTime dateToConvert = LocalDateTime.of(2010, 11, 10, 8, 20);
|
||||||
|
|
||||||
|
// when
|
||||||
|
Date date = LocalDateTimeToDateConverter.convertToDateViaInstant(dateToConvert);
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||||
|
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||||
|
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||||
|
assertEquals(8, calendar.get(Calendar.HOUR));
|
||||||
|
assertEquals(20, calendar.get(Calendar.MINUTE));
|
||||||
|
assertEquals(0, calendar.get(Calendar.SECOND));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010WhenConvertViaSqlTimestamp() {
|
||||||
|
// given
|
||||||
|
LocalDateTime dateToConvert = LocalDateTime.of(2010, 11, 10, 8, 20);
|
||||||
|
|
||||||
|
// when
|
||||||
|
Date date = LocalDateTimeToDateConverter.convertToDateViaSqlTimestamp(dateToConvert);
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||||
|
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||||
|
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||||
|
assertEquals(8, calendar.get(Calendar.HOUR));
|
||||||
|
assertEquals(20, calendar.get(Calendar.MINUTE));
|
||||||
|
assertEquals(0, calendar.get(Calendar.SECOND));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.datetime;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* JUnits for {@link LocalDateToDateConverter} class.
|
||||||
|
*
|
||||||
|
* @author abialas
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LocalDateToDateConverterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010WhenConvertViaInstant() {
|
||||||
|
// given
|
||||||
|
LocalDate dateToConvert = LocalDate.of(2010, 11, 10);
|
||||||
|
|
||||||
|
// when
|
||||||
|
Date date = LocalDateToDateConverter.convertToDateViaInstant(dateToConvert);
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||||
|
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||||
|
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturn10thNovember2010WhenConvertViaSqlDate() {
|
||||||
|
// given
|
||||||
|
LocalDate dateToConvert = LocalDate.of(2010, 11, 10);
|
||||||
|
|
||||||
|
// when
|
||||||
|
Date date = LocalDateToDateConverter.convertToDateViaSqlDate(dateToConvert);
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||||
|
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||||
|
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.java9.datetime;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class which shows a way to convert java.util.Date into java.time.LocalDate with new Java 1.9.
|
||||||
|
*
|
||||||
|
* @author abialas
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DateToLocalDateConverter {
|
||||||
|
|
||||||
|
public static LocalDate convertToLocalDate(Date dateToConvert) {
|
||||||
|
return LocalDate.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user