Wednesday 16 January 2019

How to use format(Date date, StringBuffer toAppendTo, FieldPosition pos) method of SimpleDateFormat


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

SimpleDateFormatDemo.java

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

public class SimpleDateFormatDemo
{
    public static void main(String[] args)
    {
        Date date =  new Date();
        System.out.println("date           = "+date);
       
        SimpleDateFormat formatter = new SimpleDateFormat();
        StringBuffer sb = new StringBuffer();

        /*
         * Parameters:
         *
         * date - the date-time value to be formatted into a date-time
         * string.
         *
         * toAppendTo - where the new date-time text is to be
         * appended.
         *
         * pos - the formatting position. On input: an alignment
         * field, if desired. On output: the offsets of the alignment
         * field.
         *
         * Returns:
         *
         * the formatted date-time string.
         */

        formatter.format(date, sb,
                new FieldPosition(DateFormat.DATE_FIELD));
        System.out.println("Formatted date = "+sb);
    }

}

Output:

date           = Wed Jan 09 15:37:09 IST 2019
Formatted date = 1/9/19 3:37 PM

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/4327965661b34e0cc17880946800be5f5d73f2dd/Java_2019/SimpleDateFormatDemo_format_sb_fieldpos/?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