Tuesday 15 January 2019

How to use format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) of DateFormat


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

DateFormatDemo.java

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

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

        DateFormat dateFormat = DateFormat.getDateInstance();
        StringBuffer sb = new StringBuffer();

        /*
         * Parameters:
         *
         * date - a Date to be formatted into a date/time string.
         *
         * toAppendTo - the string buffer for the returning date/time
         * string.
         *
         * fieldPosition - keeps track of the position of the field
         * within the returned string.
         *
         * Returns:
         *
         * the string buffer passed in as toAppendTo, with formatted
         * text appended.
         */

        sb = dateFormat.format(date, sb,
                new FieldPosition(DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD));
        System.out.println(sb);

    }

}

Output:

Tue Jan 01 10:40:35 IST 2019
Jan 1, 2019

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

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

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