java.text.SimpleDateFormat formatToCharacterIterator(Object obj)
Description :
This java tutorial shows how to use the formatToCharacterIterator(Object obj) method of SimpleDateFormat class of java.text package. This method Formats an Object producing an AttributedCharacterIterator. You can use the returned AttributedCharacterIterator to build the resulting String, as well as to determine information about the resulting String. Its worth to make a note that this method override the method formatToCharacterIterator in class Format.
Method Syntax :
public AttributedCharacterIterator formatToCharacterIterator(Object obj)
Parameter Input :
DataType | Parameter | Description |
---|---|---|
Object | obj | The object which we want to format |
Method Returns :
This method returns AttributedCharacterIterator which describes the formatted value.
Compatibility Version :
Requires Java 1.4 and up
Exception :
1. This exception will be thrown if the specified method argument obj is null
NullPointerException
2. if the Format’s pattern is invalid, the following exception will be thrown by this method
IllegalArgumentException
Java Code Example :
This java example source code demonstrates the use of formatToCharacterIterator(Object obj) method of SimpledDateFormat class.
Firstly we have initiallized SimpleDateformat constructor to take an argument of “MM/dd/yyyy ” expecting this as the date format output. Then after which we have formatToCharacterIterator method which returns a AttributedCharacterIterator. The CharacterIterator interface has been implemented by CharacterIterator thus we have assigned the result of this method to CharacterIterator object. To get the desired output we iterated through the contents of CharacterIterator.
package com.javatutorialhq.java.examples; import java.text.AttributedCharacterIterator; import java.text.CharacterIterator; import java.text.SimpleDateFormat; import java.util.Calendar; /* * This example source code demonstrates the use of * formatToCharacterIterator(Object obj) method * of SimpleDateFormat class */ public class FormatToCharacterIteratorExample { public static void main(String[] args) throws InterruptedException { // declare the simpledateformat object SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); // instantiate calendar object Calendar cal = Calendar.getInstance(); // get the result of the formatToCharacterIterator AttributedCharacterIterator charIterator = sdf .formatToCharacterIterator(cal.getTime()); // print the contents of our CharacterIterator for (char c = charIterator.first(); c != CharacterIterator.DONE; c = charIterator.next()) { System.out.print(c); } } }
Sample Output :
Running the formatToCharacterIterator(Object obj) method example source code of Calendar class will give you the following output:
Exception Scenario :
If we have the following declaration on our example sdf.formatToCharacterIterator(null), the following exception will be thrown
Exception in thread "main" java.lang.NullPointerException: formatToCharacterIterator must be passed non-null object at java.text.SimpleDateFormat.formatToCharacterIterator(SimpleDateFormat.java:1003) at com.javatutorialhq.java.examples.FormatToCharacterIteratorExample.main(FormatToCharacterIteratorExample.java:25)
Similar Method :
- N/A