How to convert java.time.LocalDate to java.util.Date?

The following code snippet demonstrate how to convert java.time.LocalDate to java.util.Date and vice versa. In the first part of the code snippet we convert LocalDate to Date and back to LocalDate object. On the second part we convert LocalDateTime to Date and back to LocalDateTime object.

package org.kodejava.datetime;

import java.time.*;
import java.util.Date;

public class LocalDateToDate {
    public static void main(String[] args) {
        // Convert java.time.LocalDate to java.util.Date and back to
        // java.time.LocalDate
        LocalDate localDate =;
        System.out.println("LocalDate = " + localDate);

        Date date1 = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.println("Date      = " + date1);

        localDate = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        System.out.println("LocalDate = " + localDate);

        // Convert java.time.LocalDateTime to java.util.Date and back to
        // java.time.LocalDateTime
        LocalDateTime localDateTime =;
        System.out.println("LocalDateTime = " + localDateTime);

        Date date2 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("Date          = " + date2);

        localDateTime = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        System.out.println("LocalDateTime = " + localDateTime);

The result of the code snippet:

LocalDate = 2021-11-20
Date      = Sat Nov 20 00:00:00 CST 2021
LocalDate = 2021-11-20

LocalDateTime = 2021-11-20T18:25:05.706380200
Date          = Sat Nov 20 18:25:05 CST 2021
LocalDateTime = 2021-11-20T18:25:05.706

How do I convert varargs to an array?

Varargs can be seen as a simplification of array when we need to pass a multiple value as a method parameter. Varargs itself is an array that automatically created, for these reason you will be enabled to do things you can do with array to varargs.

In the example below you can see the messages parameter can be assigned to the String array variables, we can call the length method to the messages parameter as we do with the array. So actually you don’t need to convert varargs to array because varargs is array.

package org.kodejava.lang;

public class VarargsToArray {
    public static void main(String[] args) {
        printMessage("Hello ", "there", ", ", "how ", "are ", "you", "?");

    public static void printMessage(String... messages) {
        String[] copiedMessage = messages;
        for (int i = 0; i < messages.length; i++) {

How do I convert string to an integer or number?

package org.kodejava.lang;

public class StringToInteger {
    public static void main(String[] args) {
        // Some random selected number, could representing a decimal,
        // hexadecimal or octal number.
        String myLuckyNumber = "13";

        // We convert a string to an integer by invoking parseInt() method
        // of the Integer class.
        int number = Integer.parseInt(myLuckyNumber);
        System.out.println("My lucky number is: " + number);

        // We can also converting a string representation of a number other
        // then the decimal base, for instance an hexadecimal by providing
        // the radix to the method.
        number = Integer.parseInt(myLuckyNumber, 16);
        System.out.println("My lucky number is: " + number);

        number = Integer.parseInt(myLuckyNumber, 8);
        System.out.println("My lucky number is: " + number);

Our code results are:

My lucky number is: 13
My lucky number is: 19
My lucky number is: 11

How do I convert string to char array?

Here we have a small class that convert a string literal into an array, a character array. To do this we can simply use String.toCharArray() method.

package org.kodejava.lang;

public class StringToArrayExample {
    public static void main(String[] args) {
        // We have a string literal that contains the tag line of this blog.
        String literal = "Kode Java - Learn Java by Examples";

        // Now we want to convert or divided it into a small array of char.
        // To do this we can simply used String.toCharArray() method. This
        // method splits the string into an array of characters.
        char[] temp = literal.toCharArray();

        // Here we just iterate the char array and print it to our console.
        for (char c : temp) {

How do I convert collection to ArrayList?

package org.kodejava.util;

import java.util.ArrayList;
import java.util.LinkedList;

public class CollectionToArrayList {
    public static void main(String[] args) {
        // We create LinkedList collection type at put some values
        // in it. Here we put A, B, C and D letter into it.
        LinkedList<String> linkedList = new LinkedList<>();

        // Let say you want to convert it to other type of collection,
        // for instance here we convert it into ArrayList. To do it
        // we can pass the collection created above as a parameter to
        // ArrayList constructor.
        ArrayList<String> arrayList = new ArrayList<>(linkedList);

        // Now we have converted the collection into ArrayList and
        // printed what is inside.
        for (String s : arrayList) {
            System.out.println("s = " + s);