Java DateFormat setLenient() Example
DateFormat class setLenient() method example. This example shows you how to use setLenient() method.
Syntax is : public void setLenient(boolean lenient)
This method specifies whether or not date/time parsing is to be lenient. With lenient parsing, the parser may use heuristics to interpret
inputs that do not precisely match this object's format. With strict parsing, inputs must match this object's format.
Here is the code.
/**
* @(#) SetLenientDateFormat.java
* A class representing use of method setLenient() of DateFormat
class in java.text Package.
* @Version 13-May-2008
* @author Rose India Team
*/
import java.util.*;
import java.text.*;
class SetLenientDateFormat {
public static void main(String[] args) {
// Create new Date object that will be initialized to the current date.
Date date = new Date();
System.out.println("Date by Date class : " + date.toString());
// Create new DateFormat object that will returns default date/time.
DateFormat dateFormat = DateFormat.getInstance();
// format method call.
String stringDate = dateFormat.format(date);
System.out.println("Date by DateFormat class : " + stringDate);
// check that Date/time parsing is lenient or not.
boolean bool = dateFormat.isLenient();
System.out.println("Date/time parsing is lenient......" + bool);
// setLenient method call.
dateFormat.setLenient(false);
bool = dateFormat.isLenient();
System.out.println("After setLenient, Date/time parsing is lenient...."
+ bool);
}
} |
Output of the program.
Date by Date class : Fri Jun 13 18:39:30 GMT+05:30 2008
Date by DateFormat class : 6/13/08 6:39 PM
Date/time parsing is lenient......true
After setLenient, Date/time parsing is lenient....false |
|