Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -43,19 +43,19 @@
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.ws</groupId>
|
||||
<artifactId>jaxws-ri</artifactId>
|
||||
<version>2.3.3</version>
|
||||
<version>${jaxws-ri.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
<version>${jstl.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -117,7 +117,9 @@
|
||||
<properties>
|
||||
<spring.version>5.3.25</spring.version>
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
|
||||
<jstl.version>1.2</jstl.version>
|
||||
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
|
||||
<jaxws-ri.version>2.3.3</jaxws-ri.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -241,8 +241,6 @@
|
||||
<httpcore.version>4.4.16</httpcore.version>
|
||||
<httpclient.version>4.5.14</httpclient.version>
|
||||
<mockserver.version>5.11.2</mockserver.version>
|
||||
<!-- maven plugins -->
|
||||
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -21,7 +21,6 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<properties>
|
||||
<javax.validation.validation-api.version>2.0.1.Final</javax.validation.validation-api.version>
|
||||
</properties>
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.xsltProcessing;
|
||||
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.io.File;
|
||||
|
||||
public class XSLTProcessorWithParametersAndOption {
|
||||
public static void transformXMLWithParametersAndOption(
|
||||
String inputXMLPath,
|
||||
String xsltPath,
|
||||
String outputHTMLPath,
|
||||
String companyName,
|
||||
boolean enableIndentation
|
||||
) throws TransformerException {
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Source xsltSource = new StreamSource(new File(xsltPath));
|
||||
Transformer transformer = transformerFactory.newTransformer(xsltSource);
|
||||
|
||||
transformer.setParameter("companyName", companyName);
|
||||
|
||||
if (enableIndentation) {
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
}
|
||||
|
||||
Source xmlSource = new StreamSource(new File(inputXMLPath));
|
||||
Result outputResult = new StreamResult(new File(outputHTMLPath));
|
||||
|
||||
transformer.transform(xmlSource, outputResult);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.xsltProcessing;
|
||||
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.io.File;
|
||||
|
||||
public class XSLTProcessorWithTemplate {
|
||||
public static void transformXMLUsingTemplate(String inputXMLPath, String xsltPath, String outputHTMLPath) throws TransformerException {
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Source xsltSource = new StreamSource(new File(xsltPath));
|
||||
Templates templates = transformerFactory.newTemplates(xsltSource);
|
||||
|
||||
Transformer transformer = templates.newTransformer();
|
||||
|
||||
Source xmlSource = new StreamSource(new File(inputXMLPath));
|
||||
Result outputResult = new StreamResult(new File(outputHTMLPath));
|
||||
|
||||
transformer.transform(xmlSource, outputResult);
|
||||
}
|
||||
}
|
||||
@@ -63,7 +63,6 @@
|
||||
<org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
|
||||
<velocity-version>1.7</velocity-version>
|
||||
<velocity-tools-version>2.0</velocity-tools-version>
|
||||
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -49,7 +49,6 @@
|
||||
|
||||
<properties>
|
||||
<asm.version>5.2</asm.version>
|
||||
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aws-s3-update-object</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aws-s3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
|
||||
@@ -122,7 +122,6 @@
|
||||
<docker.image.prefix>${azure.containerRegistry}.azurecr.io</docker.image.prefix>
|
||||
<docker-maven-plugin.version>1.1.0</docker-maven-plugin.version>
|
||||
<azure-webapp-maven-plugin.version>1.1.0</azure-webapp-maven-plugin.version>
|
||||
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -27,6 +27,7 @@
|
||||
<hsqldb.version>2.7.1</hsqldb.version>
|
||||
<spock-core.version>2.3-groovy-3.0</spock-core.version>
|
||||
<gmavenplus-plugin.version>2.1.0</gmavenplus-plugin.version>
|
||||
<maven-failsafe-plugin.version>2.21.0</maven-failsafe-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -48,7 +48,6 @@
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>14</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -53,7 +53,6 @@
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>15</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -152,7 +152,6 @@
|
||||
<awaitility.version>4.0.2</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.arraymiddle;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
|
||||
public class MiddleOfArray {
|
||||
int[] middleOfArray(int[] array) {
|
||||
if (ObjectUtils.isEmpty(array) || array.length < 3)
|
||||
return array;
|
||||
int n = array.length;
|
||||
int mid = n / 2;
|
||||
if (n % 2 == 0) {
|
||||
int mid2 = mid - 1;
|
||||
return new int[] { array[mid2], array[mid] };
|
||||
} else {
|
||||
return new int[] { array[mid] };
|
||||
}
|
||||
}
|
||||
|
||||
int[] middleOfArrayWithStartEndNaive(int[] array, int start, int end) {
|
||||
int mid = (start + end) / 2;
|
||||
int n = end - start;
|
||||
if (n % 2 == 0) {
|
||||
int mid2 = mid - 1;
|
||||
return new int[] { array[mid2], array[mid] };
|
||||
} else {
|
||||
return new int[] { array[mid] };
|
||||
}
|
||||
}
|
||||
|
||||
int[] middleOfArrayWithStartEnd(int[] array, int start, int end) {
|
||||
int mid = start + (end - start) / 2;
|
||||
int n = end - start;
|
||||
if (n % 2 == 0) {
|
||||
int mid2 = mid - 1;
|
||||
return new int[] { array[mid2], array[mid] };
|
||||
} else {
|
||||
return new int[] { array[mid] };
|
||||
}
|
||||
}
|
||||
|
||||
int[] middleOfArrayWithStartEndBitwise(int[] array, int start, int end) {
|
||||
int mid = (start + end) >>> 1;
|
||||
int n = end - start;
|
||||
if (n % 2 == 0) {
|
||||
int mid2 = mid - 1;
|
||||
return new int[] { array[mid2], array[mid] };
|
||||
} else {
|
||||
return new int[] { array[mid] };
|
||||
}
|
||||
}
|
||||
|
||||
int medianOfArray(int[] array, int start, int end) {
|
||||
Arrays.sort(array); // for safety. This can be ignored
|
||||
int mid = (start + end) >>> 1;
|
||||
int n = end - start;
|
||||
if (n % 2 == 0) {
|
||||
int mid2 = mid - 1;
|
||||
return (array[mid2] + array[mid]) / 2;
|
||||
} else {
|
||||
return array[mid];
|
||||
}
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package com.baeldung.arraymiddle;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MiddleOfArrayUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenArrayOfEvenLength_whenMiddleOfArray_thenReturn2Values() {
|
||||
int[] array = new int[100];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = i + 1;
|
||||
}
|
||||
int[] expectedMidArray = { 50, 51 };
|
||||
MiddleOfArray middleOfArray = new MiddleOfArray();
|
||||
Assert.assertArrayEquals(expectedMidArray, middleOfArray.middleOfArray(array));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfEdgeCaseLength_whenMiddleOfArray_thenReturn2Values() {
|
||||
int[] array = new int[0];
|
||||
int[] expectedMidArray = new int[0];
|
||||
MiddleOfArray middleOfArray = new MiddleOfArray();
|
||||
Assert.assertArrayEquals(expectedMidArray, middleOfArray.middleOfArray(array));
|
||||
|
||||
array = new int[] { 1, 2 };
|
||||
expectedMidArray = new int[] { 1, 2 };
|
||||
|
||||
Assert.assertArrayEquals(expectedMidArray, middleOfArray.middleOfArray(array));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfOddLength_whenMiddleOfArray_thenReturnMid() {
|
||||
int[] array = new int[99];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = i + 1;
|
||||
}
|
||||
int[] expectedMidArray = { 50 };
|
||||
MiddleOfArray middleOfArray = new MiddleOfArray();
|
||||
Assert.assertArrayEquals(expectedMidArray, middleOfArray.middleOfArray(array));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithStartAndEnd_whenMiddleOfArray_thenReturnMid() {
|
||||
int[] array = new int[100];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = i + 1;
|
||||
}
|
||||
int[] expectedMidArray = { 58 };
|
||||
MiddleOfArray middleOfArray = new MiddleOfArray();
|
||||
Assert.assertArrayEquals(expectedMidArray, middleOfArray.middleOfArrayWithStartEndNaive(array, 55, 60));
|
||||
|
||||
expectedMidArray = new int[] { 58, 59 };
|
||||
Assert.assertArrayEquals(expectedMidArray, middleOfArray.middleOfArrayWithStartEndNaive(array, 56, 60));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithStartAndEndOptimized_whenMiddleOfArray_thenReturnMid() {
|
||||
int[] array = new int[100];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = i + 1;
|
||||
}
|
||||
int[] expectedMidArray = { 78 };
|
||||
MiddleOfArray middleOfArray = new MiddleOfArray();
|
||||
Assert.assertArrayEquals(expectedMidArray, middleOfArray.middleOfArrayWithStartEnd(array, 55, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithStartAndEndBitwise_whenMiddleOfArray_thenReturnMid() {
|
||||
int[] array = new int[100];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = i + 1;
|
||||
}
|
||||
int[] expectedMidArray = { 78 };
|
||||
MiddleOfArray middleOfArray = new MiddleOfArray();
|
||||
Assert.assertArrayEquals(expectedMidArray, middleOfArray.middleOfArrayWithStartEndBitwise(array, 55, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithStartAndEnd_whenMedianOfArray_thenReturnMid() {
|
||||
int[] array = new int[100];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = i + 1;
|
||||
}
|
||||
int expectMedian = 50;
|
||||
MiddleOfArray middleOfArray = new MiddleOfArray();
|
||||
Assert.assertEquals(expectMedian, middleOfArray.medianOfArray(array, 0, 100));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-list-5</artifactId>
|
||||
<name>core-java-collections-list-5</name>
|
||||
|
||||
+4
@@ -58,4 +58,8 @@ public class DataQueue {
|
||||
return queue.poll();
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getSize() {
|
||||
return queue.size();
|
||||
}
|
||||
}
|
||||
|
||||
+30
-16
@@ -1,12 +1,13 @@
|
||||
package com.baeldung.producerconsumer;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Producer implements Runnable {
|
||||
private static final Logger log = Logger.getLogger(Producer.class.getCanonicalName());
|
||||
private final DataQueue dataQueue;
|
||||
|
||||
private static int idSequence = 0;
|
||||
final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
public Producer(DataQueue dataQueue) {
|
||||
this.dataQueue = dataQueue;
|
||||
@@ -19,22 +20,38 @@ public class Producer implements Runnable {
|
||||
|
||||
public void produce() {
|
||||
while (dataQueue.runFlag) {
|
||||
while (dataQueue.isFull() && dataQueue.runFlag) {
|
||||
try {
|
||||
dataQueue.waitOnFull();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
try {
|
||||
lock.lock();
|
||||
|
||||
while (dataQueue.isFull() && dataQueue.runFlag) {
|
||||
try {
|
||||
dataQueue.waitOnFull();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!dataQueue.runFlag) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!dataQueue.runFlag) {
|
||||
break;
|
||||
}
|
||||
Message message = generateMessage();
|
||||
dataQueue.add(message);
|
||||
dataQueue.notifyAllForEmpty();
|
||||
|
||||
Message message = generateMessage();
|
||||
dataQueue.add(message);
|
||||
dataQueue.notifyAllForEmpty();
|
||||
|
||||
log.info("Size of the queue is: " + dataQueue.getSize());
|
||||
|
||||
}
|
||||
finally{
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
//Sleeping on random time to make it realistic
|
||||
ThreadUtil.sleep((long) (Math.random() * 100));
|
||||
}
|
||||
|
||||
log.info("Producer Stopped");
|
||||
}
|
||||
|
||||
@@ -43,9 +60,6 @@ public class Producer implements Runnable {
|
||||
log.info(String.format("[%s] Generated Message. Id: %d, Data: %f%n",
|
||||
Thread.currentThread().getName(), message.getId(), message.getData()));
|
||||
|
||||
//Sleeping on random time to make it realistic
|
||||
ThreadUtil.sleep((long) (message.getData() * 100));
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
+5
-4
@@ -36,8 +36,8 @@ public class ProducerConsumerDemonstrator {
|
||||
|
||||
public static void demoMultipleProducersAndMultipleConsumers() {
|
||||
DataQueue dataQueue = new DataQueue(MAX_QUEUE_CAPACITY);
|
||||
int producerCount = 3;
|
||||
int consumerCount = 3;
|
||||
int producerCount = 5;
|
||||
int consumerCount = 5;
|
||||
List<Thread> threads = new ArrayList<>();
|
||||
Producer producer = new Producer(dataQueue);
|
||||
for(int i = 0; i < producerCount; i++) {
|
||||
@@ -45,6 +45,7 @@ public class ProducerConsumerDemonstrator {
|
||||
producerThread.start();
|
||||
threads.add(producerThread);
|
||||
}
|
||||
|
||||
Consumer consumer = new Consumer(dataQueue);
|
||||
for(int i = 0; i < consumerCount; i++) {
|
||||
Thread consumerThread = new Thread(consumer);
|
||||
@@ -52,8 +53,8 @@ public class ProducerConsumerDemonstrator {
|
||||
threads.add(consumerThread);
|
||||
}
|
||||
|
||||
// let threads run for two seconds
|
||||
sleep(2000);
|
||||
// let threads run for ten seconds
|
||||
sleep(10000);
|
||||
|
||||
// Stop threads
|
||||
producer.stop();
|
||||
|
||||
@@ -37,20 +37,19 @@
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>14</maven.compiler.source.version>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<maven.compiler.target.version>14</maven.compiler.target.version>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
@@ -25,7 +25,7 @@
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<properties>
|
||||
<joda-time.version>2.12.5</joda-time.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.epochconversion;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
public class EpochToLocalDate {
|
||||
|
||||
public static void main(String[] args) {
|
||||
long millisSinceEpoch = 2131242L;
|
||||
ZoneId zoneId = ZoneId.of("Europe/Amsterdam"); // or: ZoneId.systemDefault();
|
||||
// to get All Zone Ids available, we can use the function below
|
||||
// Set<String> allZoneIds = ZoneId.getAvailableZoneIds();
|
||||
LocalDate date =
|
||||
Instant.ofEpochMilli(millisSinceEpoch)
|
||||
.atZone(zoneId)
|
||||
.toLocalDate();
|
||||
LocalDateTime time =
|
||||
Instant.ofEpochMilli(millisSinceEpoch)
|
||||
.atZone(zoneId)
|
||||
.toLocalDateTime();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.epochconversion;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
public class LocalDateToEpoch {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ZoneId zoneId = ZoneId.of("Europe/Amsterdam"); // or: ZoneId.systemDefault()
|
||||
LocalDate date = LocalDate.now();
|
||||
long epochMilliSecondsAtDate = date.atStartOfDay(zoneId).toInstant().toEpochMilli();
|
||||
|
||||
// epoch for time
|
||||
LocalDateTime time = LocalDateTime.parse("2019-11-15T13:15:30");
|
||||
long epochMilliSecondsAtTime = time.atZone(zoneId).toInstant().toEpochMilli();
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.epochconversion;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class EpochToLocalDateUnitTest {
|
||||
|
||||
@Test
|
||||
void givenEpoch_thenComputeLocalDateCorrectly() {
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
LocalDate expectedDate = LocalDate.of(1995, 4, 15);
|
||||
long date = expectedDate.atStartOfDay(zoneId).toInstant().toEpochMilli();
|
||||
|
||||
LocalDate actualDate =
|
||||
Instant.ofEpochMilli(date)
|
||||
.atZone(zoneId)
|
||||
.toLocalDate();
|
||||
assertEquals(expectedDate, actualDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenEpoch_thenComputeLocalDateTimeCorrectly() {
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
LocalDateTime expectedTime = LocalDateTime.parse("2019-11-15T13:15:30");
|
||||
long time = expectedTime.atZone(zoneId).toInstant().toEpochMilli();
|
||||
|
||||
LocalDateTime actualTime =
|
||||
Instant.ofEpochMilli(time)
|
||||
.atZone(zoneId)
|
||||
.toLocalDateTime();
|
||||
assertEquals(expectedTime, actualTime);
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.epochconversion;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class LocalDateTimeToEpochUnitTest {
|
||||
|
||||
@Test
|
||||
void givenDate_thenComputeEpochCorrectly() {
|
||||
ZoneId zoneId = ZoneId.of("Europe/Tallinn");
|
||||
long expectedEpoch = LocalDate.now().toEpochDay();
|
||||
LocalDateTime givenDate = Instant.ofEpochMilli(expectedEpoch)
|
||||
.atZone(zoneId)
|
||||
.toLocalDateTime();
|
||||
|
||||
long actualEpoch = givenDate.atZone(zoneId).toInstant().toEpochMilli();
|
||||
assertEquals(expectedEpoch, actualEpoch);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTime_thenComputeEpochCorrectly() {
|
||||
ZoneId zoneId = ZoneId.of("Europe/Amsterdam");
|
||||
long expectedEpoch = Instant.now().toEpochMilli();
|
||||
LocalDateTime givenTime = Instant.ofEpochMilli(expectedEpoch)
|
||||
.atZone(zoneId)
|
||||
.toLocalDateTime();
|
||||
|
||||
long actualEpoch = givenTime.atZone(zoneId).toInstant().toEpochMilli();
|
||||
assertEquals(expectedEpoch, actualEpoch);
|
||||
}
|
||||
}
|
||||
@@ -132,7 +132,6 @@
|
||||
<!-- Mime Type Libraries -->
|
||||
<tika.version>2.8.0</tika.version>
|
||||
<jmime-magic.version>0.1.5</jmime-magic.version>
|
||||
<maven-jar-plugin.version>3.3.0</maven-jar-plugin.version>
|
||||
<fscontext.version>4.4.2</fscontext.version>
|
||||
<jakarta-activation-api.version>2.1.2</jakarta-activation-api.version>
|
||||
<angus-activation.version>2.0.1</angus-activation.version>
|
||||
|
||||
@@ -275,7 +275,6 @@
|
||||
<!-- maven plugins -->
|
||||
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
||||
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
|
||||
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
|
||||
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.stopexecution;
|
||||
|
||||
public class InterruptThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
while (!isInterrupted()) {
|
||||
if (isInterrupted()) {
|
||||
break;
|
||||
}
|
||||
// business logic
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user