Thursday 10 January 2019

How to format the date using Date time formatter with date style and locale[java.text.DateFormat]


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

DateFormatDemo.java

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

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

        /*
         * Parameters:
         *
         * dateStyle - the given date formatting style. For example,
         * SHORT for "M/d/yy" in the US locale.
         *
         * timeStyle - the given time formatting style. For example,
         * SHORT for "h:mm a" in the US locale.
         *
         * Returns:
         *
         * a date/time formatter.
         */

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

        dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        strDate = dateFormat.format(date);
        System.out.println("MEDIUM format = " + strDate);

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

Output:

date = Mon Dec 31 09:54:22 IST 2018
SHORT format = 12/31/18 9:54 AM
MEDIUM format = Dec 31, 2018 9:54:22 AM
LONG format = December 31, 2018 9:54:22 AM IST

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/80d90bcf5505788c0f100e330fb6b354d3132ddb/Java_2019/DateFormatDemo_getDateTimeInstance_date_time_style/?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