devxlogo

Java

Converting a Date to a Certain String Format

The format() method from SimpleDateFormat class helps to convert a Date object into certain format String objects by using the supplied pattern string. Date today = new Date();SimpleDateFormat dateFormat =

Usage of Ordinal in Enum

The ordinal() method on the enum returns the constant. In other words, you can assume this to be the same as index and similar to arrays the index starts with

Object Casting in Java

In Java, objects can be cast explicitly and implicitly: implicit casting happens when the source type extends or implements the target type (casting to a superclass or interface). explicit casting

Creating Temp Files Using Java

At times, you need to create temporary files to perform certain actions. The following code segment illustrates the same: import java.io.*;public class CreateTempFile{ public static void main(String args[]) { CreateTempFile

Calculate the Difference Between Two LocalDates

Use LocalDate and ChronoUnit: LocalDate date1 = LocalDate.of(2018, 8, 10); LocalDate date2 = LocalDate.of(2018, 8, 30); As long as the method between of the ChronoUnit enumerator takes 2 Temporals as

Get all the IP Addresses to Which a Device is Exposed

At times, your device can be exposed using multiple interface cards. The following code sample will help identify various interfaces and the related IP configuration available on them. import java.net.*;import

Convert an Array of Strings to a List

The java.util.Arrays class has an asList method to convert the argument array into a list. This will be handy when there are large amounts of information that you need to

Using Spliterator to Split the Contents of a List

Instead of just traversing a list, this is a newer way of splitting and then iterating the list: import java.util.Arrays;import java.util.List;import java.util.Spliterator;public class UsingSpliterator{ public static void main(String args[]) {