What is the use of soft?

Here lenient used in Java DateFormat . I checked the document, but did not understand what he was saying.

Can any body tell me what is the use of lenient , with one real-time example, where do we use it?

+43
java date datetime
Sep 30 2018-11-11T00:
source share
7 answers

javadoc clearly states:

Specify whether to parse date and time parsing. With soft parsing, the analyzer can use heuristics to interpret inputs that do not exactly match this object. With strict parsing, Inputs must match this object.

So, if you have a template and create a date object that strictly matches your template, set lenient to false . Also, by default, DateFormat is lenient.

Basically, DateFormat sets Calendar.setLenient , and Javadoc:

Determines if the date / time interpretation is soft. With a mild interpretation, a date such as “February 942, 1996” would be considered equivalent to the 941st day after February 1, 1996. With a strict (non-friendly) interpretation, such dates would be an exception. The default value is soft.

+40
Sep 30 2018-11-11T00:
source share

For example:

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy"); System.out.println(simpleDateFormat.parse("0")); simpleDateFormat.setLenient(false); System.out.println(simpleDateFormat.parse("0")); 

leads to:

 Thu Jan 01 00:00:00 CET 1 Exception in thread "main" java.text.ParseException: Unparseable date: "0" at java.text.DateFormat.parse(Unknown Source) at net.java.quickcheck.generator.support.X.main(X.java:28) 
+20
Sep 30 '11 at 5:53
source share

My advice is to always disconnect. I can't think of a case where you want to be lenient, and this parameter should never have been used by default for classes like SimpleDateFormat. Lenient processing can interpret garbage as valid time strings and open errors that can be difficult to catch during testing. Also, if you use lenient to tolerate changes in the time format, you are about to be burned. For example:

 System.out.println(new SimpleDateFormat("yyyyMMdd").parse("2010-12-30")); 

Holds this (your time zone may vary):

 Mon Nov 02 00:00:00 EST 2009 

This absurd result is minus one month ("-1"), the second day ("2-") of 2010. Zero month is December!

Unfortunately, using setLenient (false) does not lead to a strict interpretation of the template. SimpleDateFormat will tolerate garbage after matching the pattern, as described here:

SimpleDateFormat.parse () ignores the number of characters in the template

In addition, it is not strict with respect to the number of character patterns, such as “d” instead of “dd”:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d"); sdf.setLenient(false); System.out.println("For 5: " + sdf.parse("2010/01/5")); System.out.println("For 05: " + sdf.parse("2010/01/05")); System.out.println("For 15: " + sdf.parse("2010/01/15")); 

Productivity:

 For 5: Tue Jan 05 00:00:00 EST 2010 For 05: Tue Jan 05 00:00:00 EST 2010 For 15: Fri Jan 15 00:00:00 EST 2010 

Also with setLenient (false) "2010/01/5" the template "yyyy / MM / dd" is accepted. And data disagreement is ignored, for example, "1999/2011" with the template "yyyy / yyyy" (answer - 2011).

Using SimpleDateFormat to check date / time strings is unfortunately not reliable. If you follow the link above, you will see some solutions, including a more rigorous version of SimpleDateFormat, written by me!

+16
Oct 22 '13 at 14:34
source share

If the date is not soft, it will cause an error if you go out of range, but if not, then it will accept and correct it. for example, August 61st from the comment above will be September 30th. Java doc on how to install it. The default value is true.

+10
Sep 30 '11 at 5:57
source share

The DateFormat object is soft by default.

Drawback (Javadoc - Calendar)

The calendar has two modes for interpreting calendar fields, soft and not lenient. When the Calendar is in soft mode, it takes on a wider range of calendar field values ​​than it produces. When the calendar recalculates the values ​​of the calendar fields for return by the get () method, all calendar fields are normalized. For example, the condescending GregorianCalendar interprets MONTH == JANUARY, DAY_OF_MONTH == 32 as February 1.

When a calendar is in non-lenient mode, it throws an exception if there is any inconsistency in its calendar fields. For example, GregorianCalendar always produces DAY_OF_MONTH values ​​between 1 and the length of the month. An unwanted Gregorian calendar throws an exception when calculating its time or calendar field values, if there are any field values ​​out of range set.

+7
Sep 30 2018-11-11T00:
source share

You can set the date parser as not condescending if you want it to strictly accept the date format you provided. This is well explained in the document :

By default, parsing is soft: if the input is not in the form used by this method of the object format, but can still be parsed as a date, then the parsing completes successfully. Clients can insist on strict adherence to the format by calling setLenient(false) .

+4
Sep 30 2018-11-11T00:
source share

A disadvantage means whether a strict rule will apply when parsing. If the DateFormat object is lenient, it will take the value 32, 2005. In fact, it will be free to convert it before February 1, 2006. By default, the DateFormat object is soft.

 import java.text.DateFormat; import java.text.ParseException; import java.util.Date; public class MainClass { public static void main(String[] args) { DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat mediumDf = DateFormat.getDateInstance(DateFormat.MEDIUM); DateFormat longDf = DateFormat.getDateInstance(DateFormat.LONG); DateFormat fullDf = DateFormat.getDateInstance(DateFormat.FULL); System.out.println(shortDf.format(new Date())); System.out.println(mediumDf.format(new Date())); System.out.println(longDf.format(new Date())); System.out.println(fullDf.format(new Date())); // parsing try { Date date = shortDf.parse("Jan 32, 2005"); } catch (ParseException e) { } } } 

And the result:

 1/26/07 Jan 26, 2007 January 26, 2007 Friday, January 26, 2007 
+1
Apr 24 '17 at 10:38 on
source share



All Articles