How do I convert milliseconds value to date?

package org.kodejava.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MillisecondsToDate {
    public static void main(String[] args) {
        // Create a DateFormatter object for displaying date information.
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");

        // Get date and time information in milliseconds
        long now = System.currentTimeMillis();

        // Create a calendar object that will convert the date and time value
        // in milliseconds to date. We use the setTimeInMillis() method of the
        // Calendar object.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(now);

        System.out.println(now + " = " + formatter.format(calendar.getTime()));
    }
}

The output of the code snippet above is:

1632868662762 = 29/09/2021 06:37:42.762

How do I set default Locale?

package org.kodejava.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class DefaultLocaleExample {
    public static void main(String[] args) {
        // Use Random class to generate some random number
        Random random = new Random();

        // We use the system default locale to format a number and a date.
        NumberFormat formatter = new DecimalFormat();
        Locale locale = Locale.getDefault();
        System.out.println("Default Locale = " + locale);
        System.out.println("Number         = " + formatter.format(random.nextDouble()));
        System.out.println("Date           = " + new SimpleDateFormat().format(new Date()));

        // We change the default locale to Locale.ITALY by setting it through 
        // the Locale.setDefault() method, and then we format another number
        // and date using a new locale. This change will affect all the class 
        // that aware to the Locale, such as the NumberFormat class.
        Locale.setDefault(Locale.ITALY);
        NumberFormat newFormatter = new DecimalFormat();
        System.out.println("New Locale     = " + Locale.getDefault());
        System.out.println("Number         = " + newFormatter.format(random.nextDouble()));
        System.out.println("Date           = " + new SimpleDateFormat().format(new Date()));
    }
}

The result of the code snippet above are:

Default Locale = en_US
Number         = 0.557
Date           = 9/26/21, 12:05 PM
New Locale     = it_IT
Number         = 0,217
Date           = 26/09/21, 12:05

How do I convert string date to long value?

package org.kodejava.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringDateToLong {
    public static void main(String[] args) {
        // Here we have a string date, and we want to covert it to long value
        String today = "25/09/2021";

        // Create a SimpleDateFormat which will be used to convert the string to
        // a date object.
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        try {
            // The SimpleDateFormat parse the string and return a date object.
            // To get the date in long value just call the getTime method of
            // the Date object.
            Date date = formatter.parse(today);
            long dateInLong = date.getTime();

            System.out.println("Date         = " + date);
            System.out.println("Date in Long = " + dateInLong);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

The result of the code snippet:

Date         = Sat Sep 25 00:00:00 CST 2021
Date in Long = 1632499200000

How do I format a date-time value?

In the DateFormat class there are some predefined constants that we can use to format a date time value. Here is an example of it.

package org.kodejava.text;

import java.text.DateFormat;
import java.util.Date;

public class DefaultDateFormatExample {
    public static void main(String[] args) {
        Date date = new Date();

        // Format date in a short format
        String today = DateFormat.getDateTimeInstance(DateFormat.SHORT,
                DateFormat.SHORT).format(date);
        System.out.println("Today " + today);

        // Format date in a medium format
        today = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM).format(date);
        System.out.println("Today " + today);

        // Format date in a long format
        today = DateFormat.getDateTimeInstance(DateFormat.LONG,
                DateFormat.LONG).format(date);
        System.out.println("Today " + today);
    }
}

And you’ll see the result as follows:

Today 9/24/21 9:40 AM
Today Sep 24, 2021 9:40:28 AM
Today September 24, 2021 9:40:28 AM CST

How do I format a date into dd/MM/yyyy?

Formatting how data should be displayed on the screen is a common requirement when creating a program or application. Displaying information in a good and concise format can be an added values to the users of the application. In the following code snippet we will learn how to format a date into a certain display format.

For these purposes we can utilize the DateFormat and SimpleDateFormat classes from the java.text package. We can easily format a date in our program by creating an instance of SimpleDateFormat class and specify to format pattern. Calling the DateFormat.format(Date date) method will format a date into a date-time string.

You can see the details about date and time patters in the following link: Date and Time Patterns. Now, let’s see an example as shown in the code snippet below.

package org.kodejava.text;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatExample {
    public static void main(String[] args) {
        Date date = Calendar.getInstance().getTime();

        // Display a date in day, month, year format
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String today = formatter.format(date);
        System.out.println("Today : " + today);

        // Display date with day name in a short format
        formatter = new SimpleDateFormat("EEE, dd/MM/yyyy");
        today = formatter.format(date);
        System.out.println("Today : " + today);

        // Display date with a short day and month name
        formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
        today = formatter.format(date);
        System.out.println("Today : " + today);

        // Formatting date with full day and month name and show time up to
        // milliseconds with AM/PM
        formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
        today = formatter.format(date);
        System.out.println("Today : " + today);
    }
}

Let’s view what we got on the console:

Today : 24/09/2021
Today : Fri, 24/09/2021
Today : Fri, 24 Sep 2021
Today : Friday, 24 September 2021, 09:36:32.724 AM