Tuesday, 15 January 2019

How to use getTimeInstance methods of java.text.DateFormat class | How to format the date and time


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

DateFormatDemo1.java

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

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

        Date date = new Date();
        System.out.println(date);

        /*
         * Returns:a time formatter.
         */

        DateFormat dateFormat = DateFormat.getTimeInstance();

        String formatedDateStr = dateFormat.format(date);
        System.out.println(formatedDateStr);

    }

}

Output:

Wed Jan 02 09:29:10 IST 2019
9:29:10 AM

DateFormatDemo2.java

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

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

        DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.LONG);

        String formatedDateStr = dateFormat.format(date);
        System.out.println("formatedDateStr Long   = "+formatedDateStr);
       
        dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);

        formatedDateStr = dateFormat.format(date);
        System.out.println("formatedDateStr MEDIUM = "+formatedDateStr);
       
        dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);

        formatedDateStr = dateFormat.format(date);
        System.out.println("formatedDateStr SHORT  = "+formatedDateStr);

    }
}

Output:

Wed Jan 02 09:29:18 IST 2019
formatedDateStr Long   = 9:29:18 AM IST
formatedDateStr MEDIUM = 9:29:18 AM
formatedDateStr SHORT  = 9:29 AM

DateFormatDemo3.java

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

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

        Locale locale = Locale.CANADA;
        System.out.println(locale);

        /*
         * Parameters:
         *
         * style - the given formatting style. For example, SHORT for
         * "h:mm a" in the US locale.
         *
         * aLocale - the given locale.
         *
         * Returns:
         *
         * a time formatter.
         */

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

        String formatedDateStr = dateFormat.format(date);
        System.out.println(formatedDateStr);

    }
}

Output:

Wed Jan 02 09:29:34 IST 2019
en_CA
9:29:34 IST AM

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

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

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