Tuesday 15 January 2019

How to use isLenient() method of java.text.DateFormat class | Datetimeformatter in java


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

DateFormatDemo.java

import java.text.DateFormat;

public class DateFormatDemo
{
    public static void main(String[] args)
    {
        DateFormat dateFormat = DateFormat.getDateTimeInstance();
        System.out.println(dateFormat.getCalendar());

        /*
         * Returns:true if the calendar is lenient; false otherwise.
         */

        System.out.println(dateFormat.isLenient());

        /*
         * Specify 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.
         *
         * Parameters:
         *
         * lenient - when true, parsing is lenient
         */

        dateFormat.setLenient(false);

        System.out.println(dateFormat.getCalendar());
        System.out.println(dateFormat.isLenient());

    }
}

Output:

java.util.GregorianCalendar[time=-978121818376,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1939,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=3,DAY_OF_YEAR=3,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=9,HOUR_OF_DAY=9,MINUTE=23,SECOND=1,MILLISECOND=624,ZONE_OFFSET=21200000,DST_OFFSET=0]
true
java.util.GregorianCalendar[time=-978121818376,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1939,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=3,DAY_OF_YEAR=3,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=9,HOUR_OF_DAY=9,MINUTE=23,SECOND=1,MILLISECOND=624,ZONE_OFFSET=21200000,DST_OFFSET=0]
false

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

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

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