Thursday 10 January 2019

java.text.DateFormat class - How to format the date using Date formatter in java


Click here to watch on Youtube:
https://www.youtube.com/watch?v=xYL1mG4sHic&list=UUhwKlOVR041tngjerWxVccw

DateFormatDemo1.java

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

public class DateFormatDemo1
{
    public static void main(String[] args)
    {
        Date currentDate = new Date();
        System.out.println("Current Date = " + currentDate);

        /*
         * Returns:
         *
         * a date formatter.
         */

        DateFormat dateFormat = DateFormat.getDateInstance();

        /*
         * Parameters:
         *
         * date - the time value to be formatted into a time string.
         *
         * Returns:
         *
         * the formatted date/time string.
         */

        String dateToStr = dateFormat.format(currentDate);
        System.out.println("Formatted Date = " + dateToStr);
    }
}

Output:

en_US
December 27, 2018

DateFormatDemo2.java

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

public class DateFormatDemo2
{
    public static void main(String[] args)
    {
        Date date = new Date();
        /*
         * Parameters:
         *
         * style - the given formatting style. For example, SHORT for
         * "M/d/yy" in the US locale.
         *
         * Returns:
         *
         * a date formatter.
         */

        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT);
        String strDate = dateFormat.format(date);
        System.out.println("DEFAULT format = "+strDate);

        dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
        strDate = dateFormat.format(date);
        System.out.println("FULL format = "+strDate);

        dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
        strDate = dateFormat.format(date);
        System.out.println("LONG format = "+strDate);

        dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
        strDate = dateFormat.format(date);
        System.out.println("SHORT format = "+strDate);

    }
}

Output:

DEFAULT format = Dec 27, 2018
FULL format = Thursday, December 27, 2018
LONG format = December 27, 2018
SHORT format = 12/27/18

DateFormatDemo3.java

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

public class DateFormatDemo3
{
    public static void main(String[] args)
    {

        Locale locale = Locale.getDefault();
        System.out.println(locale);
        /*
         * Parameters:
         *
         * style - the given formatting style. For example, SHORT for
         * "M/d/yy" in the US locale. aLocale - the given locale.
         *
         * aLocale the given locale.
         *
         * Returns:
         *
         * a date formatter.
         */

        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG,
                locale);

        String strDate = dateFormat.format(new Date());
        System.out.println(strDate);

    }
}

Output:

en_US
December 27, 2018

Click the below link to download the code:
https://sites.google.com/site/javaspringram2019/java_spring_2019/DateFormatDemo_format.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java_Spring_2019/tree/master/Java_2019/DateFormatDemo_format

Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/80d90bcf5505788c0f100e330fb6b354d3132ddb/Java_2019/DateFormatDemo_format/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment