Merge branch 'eugenp:master' into master
This commit is contained in:
+10
-7
@@ -64,13 +64,13 @@ core-java-io/hard_link.txt
|
||||
core-java-io/target_link.txt
|
||||
core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF
|
||||
ethereum/logs/
|
||||
jmeter/src/main/resources/*-JMeter.csv
|
||||
jmeter/src/main/resources/*-Basic*.csv
|
||||
jmeter/src/main/resources/*-JMeter*.csv
|
||||
jmeter/src/main/resources/*ReportsDashboard*.csv
|
||||
jmeter/src/main/resources/dashboard/*ReportsDashboard*.csv
|
||||
jmeter/src/main/resources/*FileExtractionExample.csv
|
||||
jmeter/src/main/resources/*_Summary-Report.csv
|
||||
testing-modules/jmeter/src/main/resources/*-JMeter.csv
|
||||
testing-modules/jmeter/src/main/resources/*-Basic*.csv
|
||||
testing-modules/jmeter/src/main/resources/*-JMeter*.csv
|
||||
testing-modules/jmeter/src/main/resources/*ReportsDashboard*.csv
|
||||
testing-modules/jmeter/src/main/resources/dashboard/*ReportsDashboard*.csv
|
||||
testing-modules/jmeter/src/main/resources/*FileExtractionExample.csv
|
||||
testing-modules/jmeter/src/main/resources/*_Summary-Report.csv
|
||||
|
||||
ninja/devDb.mv.db
|
||||
|
||||
@@ -128,3 +128,6 @@ persistence-modules/neo4j/data/**
|
||||
/deep-shallow-copy/.mvn/wrapper
|
||||
/deep-shallow-copy/mvnw
|
||||
/deep-shallow-copy/mvnw.cmd
|
||||
|
||||
#spring-5-webflux-2
|
||||
**/testdb.mv.db
|
||||
@@ -1,3 +1,4 @@
|
||||
## Relevant Articles
|
||||
- [How To Convert Excel Data Into List Of Java Objects](https://www.baeldung.com/java-convert-excel-data-into-list)
|
||||
- [Expand Columns with Apache POI](https://www.baeldung.com/java-apache-poi-expand-columns)
|
||||
- [Apply Bold Text Style for an Entire Row Using Apache POI](https://www.baeldung.com/appache-poi-apply-bold-text-style-entire-row)
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
- [String Templates in Java 21](https://www.baeldung.com/java-21-string-templates)
|
||||
- [Unnamed Classes and Instance Main Methods in Java 21](https://www.baeldung.com/java-21-unnamed-class-instance-main)
|
||||
- [Unnamed Patterns and Variables in Java 21](https://www.baeldung.com/java-unnamed-patterns-variables)
|
||||
- [JFR View Command in Java 21](https://www.baeldung.com/java-flight-recorder-view)
|
||||
|
||||
@@ -10,4 +10,7 @@
|
||||
- [Representing Furthest Possible Date in Java](https://www.baeldung.com/java-date-represent-max)
|
||||
- [Retrieving Unix Time in Java](https://www.baeldung.com/java-retrieve-unix-time)
|
||||
- [Calculate Months Between Two Dates in Java](https://www.baeldung.com/java-months-difference-two-dates)
|
||||
- [Format LocalDate to ISO 8601 With T and Z](https://www.baeldung.com/java-format-localdate-iso-8601-t-z)
|
||||
- [Check if Two Date Ranges Overlap](https://www.baeldung.com/java-check-two-date-ranges-overlap)
|
||||
- [Difference between ZoneOffset.UTC and ZoneId.of(“UTC”)](https://www.baeldung.com/java-zoneoffset-utc-zoneid-of)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-datetime-java8-1)
|
||||
|
||||
@@ -12,3 +12,4 @@ This module contains articles about arrays conversion in Java
|
||||
- [Convert an ArrayList of String to a String Array in Java](https://www.baeldung.com/java-convert-string-arraylist-array)
|
||||
- [Convert Char Array to Int Array in Java](https://www.baeldung.com/java-convert-char-int-array)
|
||||
- [How to Convert Byte Array to Char Array](https://www.baeldung.com/java-convert-byte-array-char)
|
||||
- [Convert byte[] to Byte[] and Vice Versa in Java](https://www.baeldung.com/java-byte-array-wrapper-primitive-type-convert)
|
||||
|
||||
@@ -13,4 +13,5 @@
|
||||
- [Time Complexity of Java Collections Sort in Java](https://www.baeldung.com/java-time-complexity-collections-sort)
|
||||
- [Check if List Contains at Least One Enum](https://www.baeldung.com/java-list-check-enum-presence)
|
||||
- [Comparison of for Loops and Iterators](https://www.baeldung.com/java-for-loops-vs-iterators)
|
||||
- [PriorityQueue iterator() Method in Java](https://www.baeldung.com/java-priorityqueue-iterator)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-4)
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
- [Removing the Last Node in a Linked List](https://www.baeldung.com/java-linked-list-remove-last-element)
|
||||
- [Call a Method on Each Element of a List in Java](https://www.baeldung.com/java-call-method-each-list-item)
|
||||
- [Sorting One List Based on Another List in Java](https://www.baeldung.com/java-sorting-one-list-using-another)
|
||||
- [Reset ListIterator to First Element of the List in Java](https://www.baeldung.com/java-reset-listiterator)
|
||||
|
||||
@@ -5,6 +5,6 @@
|
||||
- [How to Get First or Last Entry From a LinkedHashMap in Java](https://www.baeldung.com/java-linkedhashmap-first-last-key-value-pair)
|
||||
- [How to Write and Read a File with a Java HashMap](https://www.baeldung.com/java-hashmap-write-read-file)
|
||||
- [Limiting the Max Size of a HashMap in Java](https://www.baeldung.com/java-hashmap-size-bound)
|
||||
- [How to Sort LinkedHashMap By Values in Java](https://www.baeldung.com/java-sort-linkedhashmap-using-values)
|
||||
- [How to Sort LinkedHashMap by Values in Java](https://www.baeldung.com/java-sort-linkedhashmap-using-values)
|
||||
- [How to Increment a Map Value in Java](https://www.baeldung.com/java-increment-map-value)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-6)
|
||||
|
||||
@@ -8,3 +8,4 @@
|
||||
- [System.console() vs. System.out](https://www.baeldung.com/java-system-console-vs-system-out)
|
||||
- [How to Log to the Console in Color](https://www.baeldung.com/java-log-console-in-color)
|
||||
- [Create Table Using ASCII in a Console in Java](https://www.baeldung.com/java-console-ascii-make-table)
|
||||
- [Printing Message on Console without Using main() Method in Java](https://www.baeldung.com/java-no-main-print-message-console)
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
This module contains articles about date operations in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Calculate Number of Weekdays Between Two Dates in Java](https://www.baeldung.com/java-count-weekdays-between-two-dates)
|
||||
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.stringdatetoxmlgregoriancalendar;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
|
||||
public class StringDateToXMLGregorianCalendarConverter {
|
||||
public static XMLGregorianCalendar usingDatatypeFactoryForDate(String dateAsString) throws DatatypeConfigurationException {
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(dateAsString);
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar usingLocalDate(String dateAsString) throws DatatypeConfigurationException {
|
||||
LocalDate localDate = LocalDate.parse(dateAsString);
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(localDate.toString());
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar usingSimpleDateFormat(String dateTimeAsString) throws DatatypeConfigurationException, ParseException {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
||||
Date date = simpleDateFormat.parse(dateTimeAsString);
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(simpleDateFormat.format(date));
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar usingGregorianCalendar(String dateTimeAsString) throws DatatypeConfigurationException, ParseException {
|
||||
GregorianCalendar calendar = new GregorianCalendar();
|
||||
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(dateTimeAsString));
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar usingJodaTime(String dateTimeAsString) throws DatatypeConfigurationException {
|
||||
DateTime dateTime = DateTime.parse(dateTimeAsString, DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"));
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendar(dateTime.toGregorianCalendar());
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.stringdatetoxmlgregoriancalendar;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class StringDateToXMLGregorianCalendarConverterUnitTest {
|
||||
private static final String dateAsString = "2014-04-24";
|
||||
private static final String dateTimeAsString = "2014-04-24T15:45:30";
|
||||
|
||||
@Test
|
||||
void givenStringDate_whenUsingDatatypeFactory_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingDatatypeFactoryForDate(dateAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringDateTime_whenUsingApacheCommonsLang3_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingLocalDate(dateAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringDateTime_whenUsingSimpleDateFormat_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException, ParseException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingSimpleDateFormat(dateTimeAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
assertEquals(15,xmlGregorianCalendar.getHour());
|
||||
assertEquals(45,xmlGregorianCalendar.getMinute());
|
||||
assertEquals(30,xmlGregorianCalendar.getSecond());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringDateTime_whenUsingGregorianCalendar_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException, ParseException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingGregorianCalendar(dateTimeAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
assertEquals(15,xmlGregorianCalendar.getHour());
|
||||
assertEquals(45,xmlGregorianCalendar.getMinute());
|
||||
assertEquals(30,xmlGregorianCalendar.getSecond());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStringDateTime_whenUsingJodaTime_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingJodaTime(dateTimeAsString);
|
||||
assertEquals(24,xmlGregorianCalendar.getDay());
|
||||
assertEquals(4,xmlGregorianCalendar.getMonth());
|
||||
assertEquals(2014,xmlGregorianCalendar.getYear());
|
||||
assertEquals(15,xmlGregorianCalendar.getHour());
|
||||
assertEquals(45,xmlGregorianCalendar.getMinute());
|
||||
assertEquals(30,xmlGregorianCalendar.getSecond());
|
||||
}
|
||||
}
|
||||
@@ -5,3 +5,4 @@ This module contains articles about parsing and formatting Java date and time ob
|
||||
### Relevant Articles:
|
||||
- [Convert String to Instant](https://www.baeldung.com/java-string-to-instant)
|
||||
- [Sort Date Strings in Java](https://www.baeldung.com/java-sort-date-strings)
|
||||
- [Using Current Time as Filename in Java](https://www.baeldung.com/java-current-time-filename)
|
||||
|
||||
@@ -6,3 +6,4 @@
|
||||
- [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain)
|
||||
- [Use Cases for Static Methods in Java](https://www.baeldung.com/java-static-methods-use-cases)
|
||||
- [TriFunction Interface in Java](https://www.baeldung.com/java-trifunction)
|
||||
- [Lazy Field Initialization with Lambdas](https://www.baeldung.com/java-lambda-lazy-field-initialization)
|
||||
|
||||
@@ -9,7 +9,7 @@ This module contains articles about core Java input and output (IO)
|
||||
- [SequenceInputStream Class in Java](https://www.baeldung.com/java-sequenceinputstream)
|
||||
- [Read a File Into a Map in Java](https://www.baeldung.com/java-read-file-into-map)
|
||||
- [Read User Input Until a Condition Is Met](https://www.baeldung.com/java-read-input-until-condition)
|
||||
- [Java Scanner.skip method with examples](https://www.baeldung.com/java-scanner-skip)
|
||||
- [Java Scanner.skip Method with Examples](https://www.baeldung.com/java-scanner-skip)
|
||||
- [Generate the MD5 Checksum for a File in Java](https://www.baeldung.com/java-md5-checksum-file)
|
||||
- [Getting the Filename From a String Containing an Absolute File Path](https://www.baeldung.com/java-filename-full-path)
|
||||
- [Mocking Java InputStream Object](https://www.baeldung.com/java-mocking-inputstream)
|
||||
|
||||
@@ -7,5 +7,6 @@ This module contains articles about core Java input and output (IO)
|
||||
- [How to Remove Line Breaks From a File in Java](https://www.baeldung.com/java-file-remove-line-breaks)
|
||||
- [Difference Between ZipFile and ZipInputStream in Java](https://www.baeldung.com/java-zipfile-vs-zipinputstream)
|
||||
- [How to Write Strings to OutputStream in Java](https://www.baeldung.com/java-write-string-outputstream)
|
||||
- [Read a File and Split It Into Multiple Files in Java](https://www.baeldung.com/java-read-file-split-into-several)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-4)
|
||||
|
||||
|
||||
@@ -11,5 +11,5 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
- [Quick Use of FilenameFilter](https://www.baeldung.com/java-filename-filter)
|
||||
- [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader)
|
||||
- [Difference Between FileReader and BufferedReader in Java](https://www.baeldung.com/java-filereader-vs-bufferedreader)
|
||||
- [Java: Read Multiple Inputs on Same Line](https://www.baeldung.com/java-read-multiple-inputs-same-line)
|
||||
- [Write Console Output to Text File in Java](https://www.baeldung.com/java-write-console-output-file)
|
||||
- [Read Multiple Inputs on the Same Line in Java](https://www.baeldung.com/java-read-multiple-inputs-same-line)
|
||||
- [Write Console Output to Text File in Java](https://www.baeldung.com/java-write-console-output-file)
|
||||
|
||||
@@ -13,3 +13,4 @@ This module contains articles about core features in the Java language
|
||||
- [Compress and Uncompress Byte Array Using Deflater/Inflater](https://www.baeldung.com/java-compress-uncompress-byte-array)
|
||||
- [Static Final Variables in Java](https://www.baeldung.com/java-static-final-variables)
|
||||
- [What Is the Error: “Non-static method cannot be referenced from a static context”?](https://www.baeldung.com/java-non-static-method-cannot-be-referenced-from-a-static-context)
|
||||
- [Recursively Sum the Integers in an Array](https://www.baeldung.com/java-recursive-sum-integer-array)
|
||||
|
||||
@@ -14,4 +14,7 @@
|
||||
- [Clamp Function in Java](https://www.baeldung.com/java-clamp-function)
|
||||
- [Creating a Magic Square in Java](https://www.baeldung.com/java-magic-square)
|
||||
- [Check if a Point Is Between Two Points Drawn on a Straight Line in Java](https://www.baeldung.com/java-check-point-straight-line)
|
||||
- [Validate if a String Is a Valid Geo Coordinate](https://www.baeldung.com/java-geo-coordinates-validation)
|
||||
- [Rotate a Vertex Around a Certain Point in Java](https://www.baeldung.com/java-rotate-vertex-around-point)
|
||||
- [Calculating the Power of Any Number in Java Without Using Math pow() Method](https://www.baeldung.com/java-calculating-the-power-without-math-pow)
|
||||
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-math-2)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
## Relevant Articles
|
||||
- [Check if a double Is an Integer in Java](https://www.baeldung.com/java-check-double-integer)
|
||||
- [Print a Double Value Without Scientific Notation in Java](https://www.baeldung.com/java-print-double-number-no-scientific-notation)
|
||||
- [Check if a Float Value is Equivalent to an Integer Value in Java](https://www.baeldung.com/java-float-integer-equal)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
- [Convert a Number to a Letter in Java](https://www.baeldung.com/java-convert-number-to-letter)
|
||||
- [Convert Long to BigDecimal in Java](https://www.baeldung.com/java-convert-long-bigdecimal)
|
||||
- [Convert int to Long in Java](https://www.baeldung.com/java-convert-int-long)
|
||||
- [How To Convert Double To Float In Java](https://www.baeldung.com/java-convert-double-float)
|
||||
- [How to Convert Double to Float in Java](https://www.baeldung.com/java-convert-double-float)
|
||||
- [Converting from float to BigDecimal in Java](https://www.baeldung.com/java-convert-float-bigdecimal)
|
||||
- [Convert Positive Integer to Negative and Vice Versa in Java](https://www.baeldung.com/java-negating-integer)
|
||||
- [Rounding Up a Number to Nearest Multiple of 5 in Java](https://www.baeldung.com/java-round-nearest-multiple-five)
|
||||
|
||||
@@ -9,5 +9,5 @@
|
||||
- [Regular Expression: \z vs \Z Anchors in Java](https://www.baeldung.com/java-regular-expression-z-vs-z-anchors)
|
||||
- [Extract Text Between Square Brackets](https://www.baeldung.com/java-get-content-between-square-brackets)
|
||||
- [Get the Indexes of Regex Pattern Matches in Java](https://www.baeldung.com/java-indexes-regex-pattern-matches)
|
||||
- [Check if a String is Strictly Alphanumeric With Java](https://www.baeldung.com/java-check-string-contains-only-letters-numbers)
|
||||
- [Check if a String Is Strictly Alphanumeric With Java](https://www.baeldung.com/java-check-string-contains-only-letters-numbers)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-regex)
|
||||
|
||||
@@ -7,4 +7,5 @@ This module contains articles about core Java Security
|
||||
- [Extract CN From X509 Certificate in Java](https://www.baeldung.com/java-extract-common-name-x509-certificate)
|
||||
- [Check Certificate Name and Alias in Keystore File](https://www.baeldung.com/java-keystore-check-certificate-name-alias)
|
||||
- [Using a Custom TrustStore in Java](https://www.baeldung.com/java-custom-truststore)
|
||||
- [Enable Java SSL Debug Logging](https://www.baeldung.com/java-ssl-debug-logging)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-security-3)
|
||||
|
||||
@@ -3,11 +3,14 @@
|
||||
- [How to Center Text Output in Java](https://www.baeldung.com/java-center-text-output)
|
||||
- [Capitalize the First Letter of Each Word in a String](https://www.baeldung.com/java-string-initial-capital-letter-every-word)
|
||||
- [Check if a String Contains Only Unicode Letters](https://www.baeldung.com/java-string-all-unicode-characters)
|
||||
- [Create a Mutable String in Java](https://www.baeldung.com/java-mutable-string)
|
||||
- [Create a “Mutable” String in Java](https://www.baeldung.com/java-mutable-string)
|
||||
- [Check if a String Contains a Number Value in Java](https://www.baeldung.com/java-string-number-presence)
|
||||
- [Difference Between String isEmpty() and isBlank()](https://www.baeldung.com/java-string-isempty-vs-isblank)
|
||||
- [String’s Maximum Length in Java](https://www.baeldung.com/java-strings-maximum-length)
|
||||
- [Java’s String.length() and String.getBytes().length](https://www.baeldung.com/java-string-length-vs-getbytes-length)
|
||||
- [Replace Non-Printable Unicode Characters in Java](https://www.baeldung.com/java-replace-non-printable-unicode-characters)
|
||||
- [Check If a Java StringBuilder Object Contains a Character](https://www.baeldung.com/java-check-stringbuilder-object-contains-character)
|
||||
- [Comparing One String With Multiple Values in One Expression in Java](https://www.baeldung.com/java-compare-string-multiple-values-one-expression)
|
||||
- [UTF-8 Validation in Java](https://www.baeldung.com/java-utf-8-validation)
|
||||
- [Simple Morse Code Translation in Java](https://www.baeldung.com/java-morse-code-english-translate)
|
||||
- [How to Determine if a String Contains Invalid Encoded Characters](https://www.baeldung.com/java-check-string-contains-invalid-encoded-characters)
|
||||
|
||||
@@ -29,6 +29,16 @@
|
||||
<artifactId>tsid-creator</artifactId>
|
||||
<version>5.2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>1.16.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.14.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.uuid;
|
||||
|
||||
import static org.apache.commons.codec.binary.Base64.decodeBase64;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.lang3.Conversion;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class DecodeUUIDStringFromBase64UnitTest {
|
||||
private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
|
||||
|
||||
@Test
|
||||
public void shouldDecodeUUIDUsingByteArrayAndBase64Decoder() {
|
||||
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
|
||||
byte[] decodedBytes = Base64.getDecoder()
|
||||
.decode(expectedEncodedString);
|
||||
UUID uuid = convertToUUID(decodedBytes);
|
||||
assertEquals(originalUUID, uuid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDecodeUUIDUsingByteBufferAndBase64UrlDecoder() {
|
||||
String expectedEncodedString = "zF-T94zxSlGDxudAMToMbA";
|
||||
byte[] decodedBytes = Base64.getUrlDecoder()
|
||||
.decode(expectedEncodedString);
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(decodedBytes);
|
||||
long mostSignificantBits = byteBuffer.getLong();
|
||||
long leastSignificantBits = byteBuffer.getLong();
|
||||
UUID uuid = new UUID(mostSignificantBits, leastSignificantBits);
|
||||
assertEquals(originalUUID, uuid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDecodeUUIDUsingApacheUtils() {
|
||||
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
|
||||
byte[] decodedBytes = decodeBase64(expectedEncodedString);
|
||||
UUID uuid = Conversion.byteArrayToUuid(decodedBytes, 0);
|
||||
assertEquals(originalUUID, uuid);
|
||||
}
|
||||
|
||||
private UUID convertToUUID(byte[] src) {
|
||||
long mostSignificantBits = convertBytesToLong(src, 0);
|
||||
long leastSignificantBits = convertBytesToLong(src, 8);
|
||||
|
||||
return new UUID(mostSignificantBits, leastSignificantBits);
|
||||
}
|
||||
|
||||
private long convertBytesToLong(byte[] uuidBytes, int start) {
|
||||
long result = 0;
|
||||
|
||||
for(int i = 0; i < 8; i++) {
|
||||
int shift = i * 8;
|
||||
long bits = (255L & (long)uuidBytes[i + start]) << shift;
|
||||
long mask = 255L << shift;
|
||||
result = result & ~mask | bits;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.uuid;
|
||||
|
||||
import static org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.lang3.Conversion;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class EncodeUUIDToBase64StringUnitTest {
|
||||
private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
|
||||
|
||||
@Test
|
||||
public void shouldEncodeUUIDUsingByteArrayAndBase64Encoder() {
|
||||
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
|
||||
byte[] uuidBytes = convertToByteArray(originalUUID);
|
||||
String encodedUUID = Base64.getEncoder().withoutPadding()
|
||||
.encodeToString(uuidBytes);
|
||||
assertEquals(expectedEncodedString, encodedUUID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeUUIDUsingByteBufferAndBase64UrlEncoder() {
|
||||
String expectedEncodedString = "zF-T94zxSlGDxudAMToMbA";
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
|
||||
byteBuffer.putLong(originalUUID.getMostSignificantBits());
|
||||
byteBuffer.putLong(originalUUID.getLeastSignificantBits());
|
||||
String encodedUUID = Base64.getUrlEncoder().withoutPadding()
|
||||
.encodeToString(byteBuffer.array());
|
||||
assertEquals(expectedEncodedString, encodedUUID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeUUIDUsingApacheUtils() {
|
||||
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
|
||||
byte[] bytes = Conversion.uuidToByteArray(originalUUID, new byte[16], 0, 16);
|
||||
String encodedUUID = encodeBase64URLSafeString(bytes);
|
||||
assertEquals(expectedEncodedString, encodedUUID);
|
||||
}
|
||||
|
||||
private byte[] convertToByteArray(UUID uuid) {
|
||||
byte[] result = new byte[16];
|
||||
|
||||
long mostSignificantBits = uuid.getMostSignificantBits();
|
||||
fillByteArray(0, 8, result, mostSignificantBits);
|
||||
|
||||
long leastSignificantBits = uuid.getLeastSignificantBits();
|
||||
fillByteArray(8, 16, result, leastSignificantBits);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void fillByteArray(int start, int end, byte[] result, long bits) {
|
||||
for (int i = start; i < end; i++) {
|
||||
int shift = i * 8;
|
||||
result[i] = (byte) ((int) (255L & bits >> shift));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,3 +6,4 @@ This module contains articles about Gson
|
||||
- [Solving Gson Parsing Errors](https://www.baeldung.com/gson-parsing-errors)
|
||||
- [Difference between Gson @Expose and @SerializedName](https://www.baeldung.com/gson-expose-vs-serializedname)
|
||||
- [Resolving Gson’s “Multiple JSON Fields” Exception](https://www.baeldung.com/java-gson-multiple-json-fields-exception)
|
||||
- [Using Static Methods Instead of Deprecated JsonParser](https://www.baeldung.com/java-static-methods-jsonparser-replacement)
|
||||
|
||||
@@ -9,4 +9,5 @@ This module contains articles about libraries for data processing in Java.
|
||||
- [A Guide to Apache Crunch](https://www.baeldung.com/apache-crunch)
|
||||
- [Intro to Apache Storm](https://www.baeldung.com/apache-storm)
|
||||
- [Guide to JMapper](https://www.baeldung.com/jmapper)
|
||||
More articles: [[next -->]](/../libraries-data-2)
|
||||
- [What Does It Mean to Hydrate an Object?](https://www.baeldung.com/java-object-hydration)
|
||||
- More articles: [[next -->]](/../libraries-data-2)
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Difference Between mvn install and mvn verify](https://www.baeldung.com/maven-install-versus-verify)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user