-->
Skip to main content

Posts

Showing posts from July, 2020

Convert to String from Byte Array In Java [2021]

Convert to String from Byte Array In Java [2021] To convert to String from Byte Array we can use the String class Constructors or Base64 Class method introduced in Java8. In this tutorial, we will cover how to convert to String from Byte Array in Java using 4 ways. In an earlier post, we have covered how to  Convert String to Byte Array in Java . Contents Convert to String from Byte Array in Java 1. Using new String(byte[] bytes) 2. Using new String(byte[] bytes,String charsetName) 3. Using new String(byte[] bytes,Charset charset) 4. Using Base64.getEncoder().encodeToString(byte[] src) Complete Example Conclusion References Convert to String from Byte Array in Java To get String from Byte Array in Java we will use the String class overloaded constructors and the Base64 class method introduced in Java8. Using new String(byte[] bytes) Using new String(byte[] bytes, String charsetName) throws  UnsupportedEncodingException Using new String(byte[] by...

Convert String to Byte Array In Java [2021]

Convert String to Byte Array In Java [2021]  In this tutorial, we will cover how to convert String to Byte Array in Java using 4 ways. In the recent tutorial, we have covered how to Convert to String from Byte Array in Java Contents Convert String to Byte Array in Java 1. Using getBytes() 2. Using getBytes(String charsetName) 3. Using getBytes(Charset charset) 4. Using Base64.getDecoder().decode(String src) Complete Example Conclusion References Convert String to Byte Array in Java To convert String to Byte Array in Java we will use the getBytes() overloaded method in the String class and the Base64 class method introduced in Java8. Using getBytes() Using getBytes(String charsetName) throws  UnsupportedEncodingException Using getBytes(Charset charset) Using Base64.getDecoder().decode(String src) 1. Using getBytes() This method directly converts String to byte[] array. // sample string String string = "javacodegeek.com"; System.out.println(string...

Convert between Java Byte Array to Object in Java [2021]

Convert between Java Byte Array to Object  [2021] We will cover java byte array to Object conversion and cast object to byte array in Java in this tutorial. Contents Java Byte Array to Object Cast Object to Byte Array in Java Complete Example Conclusion Java Byte Array to Object To convert Java byte array to Object we use this below code-snippet.  In this code-snippet, we are taking byte array as input parameter and converting that  byte array to ByteArrayInputStram object  and then  creating ObjectInput stream object using  ByteArrayInputStram  instance  and then  finally converting to object using ObjectInput instance readObject() method .   // To convert byteArray to object private static Object fromByteArrayToObject(byte[] byteArr) throws IOException, ClassNotFoundException { if (Objects.nonNull(byteArr)) { ByteArrayInputStream bis = new ByteArrayInputStream(byteArr); ObjectInp...

Convert Between LocalDateTime to Instant In Java [2021]

Convert Between LocalDateTime to Instant In Java [2021]  In this tutorial, we will cover how to Convert Between LocalDateTime to Instant using  localdatetime.toInstant(ZoneOffset.UTC), localdatetime.atZone(ZoneId.systemDefault()).toInstant(), localdatetime.toEpochSecond(ZoneOffset.UTC) and Instant to LocalDateTime using LocalDateTime.ofInstant(instant, ZoneId.systemDefault()) Timestamp.from(instant).toLocalDateTime() LocalDateTime provides us date-time without timezone often viewed as  year-month-day-hour-minute-second where as Instant provides us instantaneous point on the time-line.  Instant class  stores a long representing epoch-seconds and an int representing nanosecond-of-second . Earlier we recent tutorials we covered how we can convert LocalDate to Instant and Instant to LocalDate in Java. Now we see Conversion between LocalDateTime to Instant using these methods with examples. Contents Convert LocalDateTime to Instant Convert Instant t...

Convert between LocalDateTime to ZonedDateTime in Java [2021]

Convert between LocalDateTime to ZonedDateTime in Java [2021]  To Convert LocalDateTime to ZonedDateTime we must add ZoneId with LocalDateTime as ZonedDateTime has ZoneId added with the DateTime. LocalDateTime to ZonedDateTime conversion can be done  using LocalDateTime.atZone(ZoneId zone) method on localdatetime instance. Contents Convert LocalDateTime to ZonedDateTime Example 1: LocalDateTime to ZonedDateTime Example 2: LocalDateTime to ZonedDateTime UTC Example 3: LocalDateTime to ZonedDateTime Change Timezone Example 4: String to ZonedDateTime Convert ZonedDateTime to LocalDateTime Example 1: ZonedDateTime to LocalDateTime Conclusion References Convert LocalDateTime to ZonedDateTime Example 1: LocalDateTime to ZonedDateTime In this example, we are going to convert LocalDateTime instance created using LocalDateTime.now() method to ZonedDateTime instance using  ZonedDateTime. atZone() with system default zone. import java.time.LocalDateTime; ...

Java Clock class and its Methods with Examples [2021]

Java Clock class and its Methods with Examples [2021]  Java Clock class is one of the java.time package classes introduced in Java 8 which provides access to current instant, date-time using a timezone.  A Clock is a good alternative to old System.currentTimeMillis() and TimeZone.getDefault() methods. Here is the declaration of Java Clock class  public abstract class Clock extends Object The clock is an abstract class it has 4 implementing classes which are shown in below image These four classes FIxedClock, OffsetClock, SystemClock, TickClock implements Clock abstract class. Here is the list of methods that we are going to cover in this tutorial. public abstract Instant instant() public long millis() public abstract ZoneId getZone() public static Clock systemDefaultZone() public static Clock systemUTC() public static Clock system(ZoneId zone) public abstract Clock withZone(ZoneId zone) public static Clock offset(Clock baseClock,Duration offsetDuration) public stati...

Java Instant Now() Method Example In Java [2021]

Java Instant Now() Method Example In Java [2021]  Java Instant Now() method is overloaded method and has 2 variants public static Instant now() public static Instant now​(Clock clock) Instant now() directly gives the Instant instance with the default system clock. Instant now(Clock clock) gives us the liberty to change Clock and provide our own customized clock. Contents Java Instant Now 1. Java Instant now() Example 2. Java Instant now(Clock clock) Example 3. Java Instant now in UTC Timezone 4. Java Instant now with custom Timezone 5. Java Instant now Example with custom Format 6. Java Instant now Minus One Day Example 7. Java Instant now to NanoSeconds Example 8. Java Instant now to Date Example Conclusion References Java Instant Now Now we are going to see Instant class now methods examples and other Instant class examples. 1. Java Instant now() Example  This method Instant.now() gets the current instant instance from the system clock. impo...

Convert between Localdate to sql Date in Java [2021]

Convert between Localdate to SQL Date in Java [2021]  In this tutorial, we will see Java 8 examples to convert from java.time.LocalDate to SQL Date and vice versa. Contents java.time.LocalDate to java.sql.Date java.sql.Date to java.time.LocalDate Conclusion References java.time.LocalDate to java.sql.Date To Get java.sql.Date from LocalDate we will use java.sql.Date.valueOf(LocalDate localDate) method.  Example: import java.time.LocalDate; //LocalDate to sql Date public class LocalDateToSqlDateEx { public static void main(String[] args) { // LocalDate instance LocalDate localDate = LocalDate.now(); System.out.println("LocalDate : " + localDate); // Get sql Date from LocalDate instance java.sql.Date sqlDate = java.sql.Date.valueOf(localDate); System.out.println("LocalDate to sql Date : " + sqlDate); } } Output: LocalDate : 2020-07-17 LocalDate to sql Date : 2020-07-17 java.sql.Date to java.time.LocalDate To get LocalDa...

LocalDate parse string example | Convert String to LocalDate [2021]

LocalDate parse string example | Convert String to LocalDate [2021]  In this tutorial, we will see LocalDate parse string example and show how to convert string to localdate in java. There are two types of parse methods in the LocalDate Java class. LocalDate parse(CharSequence text) LocalDate parse(CharSequence text, DateTimeFormatter formatter) LocalDate Java class is an immutable and thread-safe class introduced in new Date and Time API in Java8 and date is represented in format YYYY-MM-dd. Contents Localdate Parse String to LocalDate example LocalDate Parse Multiple Formats 1. LocalDate Parse MM/dd/yyyy 2. LocalDate parse dd-MMM-yyyy 3. LocalDate parse dd/MM/yyyy 4. LocalDate parse E, MMM dd yyyy 5. LocalDate parse EEEE, MMM dd, yyyy HH:mm:ss a 6. LocalDate parse using DateTimeFormatter.ISO_LOCAL_DATE_TIME 7. LocalDate parse using DateTimeFormatter.ISO_DATE_TIME 8. LocalDate parse UTC Timezone Conclusion References Localdate Parse St...

Invalid column type: getString/getNString not implemented for class oracle.jdbc.driver.T4CBlobAccessor

Invalid column type: getString/getNString not implemented for class oracle.jdbc.driver.T4CBlobAccessor Exception Trace Invalid column type: getString not implemented for class oracle.jdbc.driver.T4CBlobAccessor When Retrieving BLOBsjava.sql.SQLException: Invalid column type: getString/getNString not implemented for class oracle.jdbc.driver.T4CBlobAccessor at oracle.jdbc.driver.Accessor.unimpl(Accessor.java:296) ~[ojdbc8-12.2.0.1.0.jar:12.2.0.1.0] at oracle.jdbc.driver.BlobAccessor.getString(BlobAccessor.java:238) ~[ojdbc8-12.2.0.1.0.jar:12.2.0.1.0] at oracle.jdbc.driver.GeneratedStatement.getString(GeneratedStatement.java:287) ~[ojdbc8-12.2.0.1.0.jar:12.2.0.1.0] at oracle.jdbc.driver.GeneratedScrollableResultSet.getString(GeneratedScrollableResultSet.java:374) ~[ojdbc8-12.2.0.1.0.jar:12.2.0.1.0] at oracle.jdbc.driver.GeneratedResultSet.getString(GeneratedResultSet.java:594) ~[ojdbc8-12.2.0.1.0.jar:12.2.0.1.0] at com.zaxxer.hikari.pool.HikariProxyResultSet.getStr...