How-to articles, tricks, and solutions about JAVA

Convert Long into Integer

To convert a Long object to an Integer object in Java, you can use the intValue() method of the Long class, which returns the value of the Long as an int.

Calendar date to yyyy-MM-dd format in java

To format a Calendar date in the yyyy-MM-dd format in Java, you can use the SimpleDateFormat class.

How to split a string with any whitespace chars as delimiters

In Java, you can use the split() method of the String class to split a string based on a regular expression.

@Autowired - No qualifying bean of type found for dependency

No qualifying bean of type found for dependency is an error message that can occur when you are using the @Autowired annotation in Spring to inject a bean dependency.

What is path of JDK on Mac ?

On macOS, the path of the JDK (Java Development Kit) is typically /Library/Java/JavaVirtualMachines/jdk<version>.jdk/Contents/Home, where <version> is the version of the JDK.

Received fatal alert: handshake_failure through SSLHandshakeException

Received fatal alert: handshake_failure is an error message that can occur during an SSL/TLS handshake.

How to convert java.util.Date to java.sql.Date?

To convert a java.util.Date object to a java.sql.Date object in Java, you can use the java.sql.Date constructor that takes a long value as an argument.

Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio

Execution failed for task :app:compileDebugJavaWithJavac is an error message that can occur when you are trying to build an Android project in Android Studio.

What is a JavaBean exactly?

In Java, a Java Bean is a class that follows a certain set of conventions.

Simple way to repeat a string

In Java, you can use the String.repeat() method to repeat a string multiple times.

UnsatisfiedDependencyException: Error creating bean with name

UnsatisfiedDependencyException is a runtime exception that is thrown when the Spring framework is unable to resolve a dependency for a bean.

Where is array's length property defined?

In JavaScript, the length property of an array is defined on the Array.prototype.

byte[] to file in Java

To write a byte[] to a file in Java, you can use the following code:

"implements Runnable" vs "extends Thread" in Java

In Java, you can create a thread in two ways: by implementing the Runnable interface or by extending the Thread class.

How do I convert a String to an InputStream in Java?

To convert a String to an InputStream in Java, you can use the ByteArrayInputStream class, which allows you to create an InputStream from a byte array.

What is this date format? 2011-08-12T20:17:46.384Z

The date format "2011-08-12T20:17:46.384Z" is an ISO 8601 extended format, which is often used for exchanging date and time information in a machine-readable format.

Can an abstract class have a constructor?

Yes, an abstract class can have a constructor in Java.

What is the difference between == and equals() in Java?

In Java, the == operator is used to compare the references of two objects to see if they point to the same object in memory.

How to get current timestamp in string format in Java? "yyyy.MM.dd.HH.mm.ss"

To get the current timestamp in string format in Java, you can use the SimpleDateFormat class and specify the desired format string.

How to read text file from classpath in Java?

To read a text file from the classpath in Java, you can use the getResourceAsStream method of the ClassLoader class to get an InputStream for the file, and then use a BufferedReader to read the contents of the file.

How to convert Strings to and from UTF8 byte arrays in Java

To convert a String to a UTF-8 encoded byte array in Java, you can use the getBytes method of the String class and specify the character encoding as "UTF-8".

Why does Java have transient fields?

In Java, the transient keyword is used to indicate that a field should not be serialized when an object is persisted to storage or when an object is transferred over a network.

Trusting all certificates using HttpClient over HTTPS

To trust all certificates when using the Apache HttpClient library to make HTTPS requests, you can create a custom X509TrustManager implementation that trusts all certificates and use it to create an SSLContext with a custom TrustStrategy.

How to iterate over a JSONObject?

To iterate over the key/value pairs in a JSONObject, you can use the keys method to get an iterator over the keys in the object, and then use the get method to get the value for each key.

Convert int to char in java

To convert an int value to a char value in Java, you can use the char type's conversion method, (char).