java.text.SimpleDateFormat format(Date date,StringBuffer toAppendTo,FieldPosition pos)

Description :

This java tutorial shows how to use the format(Date date,StringBuffer toAppendTo,FieldPosition pos) method of SimpleDateFormat class of java.text package. This method formats the given Date into a date/time string and appends the result to the given StringBuffer. This method has been specified in in class DateFormat.

Method Syntax :

public StringBuffer format(Date date,StringBuffer toAppendTo,FieldPosition pos)

Parameter Input :

 

DataType Parameter Description
Date date the date-time value to be formatted into a date-time string.
StringBuffer toAppendTo where the new date-time text is to be appended.
FieldPosition pos the formatting position

 

Method Returns :

This method returns the start of the 100-year period into which two digit years are parsed.

Compatibility Version :

Requires Java 1.2 and up

Exception :

N/A

Discussion :

As we have discussed on the method format(Date date,StringBuffer toAppendTo,FieldPosition pos), this method is to fix the issue on the parsing of date input with two digit year.

Java Code Example :

This java example source code demonstrates the use of format(Date date,StringBuffer toAppendTo,FieldPosition pos) method of SimpledDateFormat class.

package com.javatutorialhq.java.examples;

import java.text.FieldPosition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

/*
 * This example source code demonstrates the use of  
 * get2DigitYearStart() method 
 * of SimpleDateFormat class
 */

public class FormatExample {

	public static void main(String[] args) throws InterruptedException {

		// declare the simpledateformat
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		// instantiate calendar object
		Calendar cal = Calendar.getInstance();		
		System.out.println("Time 1:" + cal.getTime());
		// declare stringbuffer object which the formatted 
		// date will be appended
		StringBuffer sb = new StringBuffer("Date:");
		sdf.format(cal.getTime(), sb, new FieldPosition(0));
		System.out.println(sb);
		

	}

}

Sample Output :

Running the format(Date date,StringBuffer toAppendTo,FieldPosition pos) method example source code of Calendar class will give you the following output:

SimpleDateFormat format(Date date,StringBuffer toAppendTo,FieldPosition pos) method example

SimpleDateFormat format(Date date,StringBuffer toAppendTo,FieldPosition pos) method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :