Core java
22 February[Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList. ClassCastException is runtime exception which indicate that code has tried to cast an object to a subclass which is not an instance. Reason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList Although static method Arrays.asList() returns List, it returns […]
21 February[Fixed] java.util.HashMap$Values cannot be cast to class java.util.List
In this post, we will see how to fix error java.util.HashMap$Values cannot be cast to class java.util.List. Why HashMap values cannot be cast to list? HashMap values returns java.util.Collection and you can not cast Collection to List or ArrayList. It will throw ClassCastException in this scenario. Let’s understand with the help of example: [crayon-6a289cbdc4409050572969/] Output: […]
20 FebruaryFormat Number with Commas in Java
In this post, we will see how to format number with commas in java. How To Add Commas to Number in Java There are multiple ways to format number with commas in java. Let’s go through them. 1. Using DecimalFormat DecimalFormat can be used by providing formatting Pattern to format number with commas in java. […]
20 February[Fixed] Unable to obtain LocalDateTime from TemporalAccessor
In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get this error, when you try to convert String to LocalDateTime and Formatted String does not have time related information. Let’s understand with the help of […]
17 FebruaryConvert LocalDate to Instant in Java
In this article, we will see how to convert LocalDate to Instant in Java. Java LocalDate to Instant Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone. Using toInstant() wth ZoneId ZoneDateTime’s toInstant() method can be used to convert LocalDate to Instant. […]
17 FebruaryConvert Instant to LocalDate in Java
1. Overview In this article, we will see how to convert Instant to LocalDate in java. An Instant represents a specific moment in time in the UTC timezone, whereas a LocalDate represents a date without time or timezone information. The challenge is to extract the date part from an Instant object and represent it as […]
17 FebruaryConvert String to LocalDateTime in Java
In this article, we will see how to convert String to LocalDateTime in Java. LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default. Java String to LocalDateTime To convert String to LocalDateTime in Java, LocalDateTime provides static method […]
16 FebruaryFormat LocalDateTime to String in Java
In this article, we will see how to format LocalDateTime to String in java. Java LocalDateTime To String To format LocalDateTime to String, we can create DateTimeFormatter and pass it to LocalDateTime’s format() method. [crayon-6a289cbdc50f2300102199/] Here are steps: Get LocalDateTime instance Create DateTimeFormatter instance with specified format Pass above DateTimeFormatter to format() method to convert […]
16 FebruaryConvert Outputstream to Byte Array in Java
In this post, we will see how to convert OutputStream to Byte array in Java. Convert OutputStream to Byte array in Java Here are steps to convert OutputStream to Byte array in java. Create instance of ByteArrayOutputStream baos Write data to ByteArrayOutputStream baos Extract byte[] using toByteArray() method. [crayon-6a289cbdc51fc037438026/] Output: Print output: Java2blog Convert OutputStream […]