Small rule mode example

Source: Internet
Author: User
Tags dateformat

In a recent project, time-based judgment was used. As a result, a clip was used to identify time processing in a policy mode. At that time, a summary was omitted.

Inductive

Scenario: user input time. The format can be yyyy-mm-dd Or yyyy-mm-dd hh: Mm. Other output formats are invalid and must be determined. Field Used

Requirements
Use the yyyy-mm-dd standard to restrict user input. In some scenarios, use the yyyy-mm-dd hh: Mm standard for input and then extend it, some scenarios may use other formats.

Judge user input. Of course, you do not need to use the policy mode, but it is also possible to use the policy mode. Description

Interface: validatorinterface. Java

Import java. Text. parseexception;
Import java. util. date;

/**
* <PRE> interface for verifying whether the date format is correct, using the Policy mode </PRE>
* @ Version 1.0, 2006-01
* @ Author jackyrong
*/
Public interface validatorinterface
{
// Whether the date format is correct
Public Boolean isvalidatetime (string S );

// Convert string to date type
Public date convertodate (string s) throws parseexception;
}

Specific usage classes of Policy 1:
Import java. Text. parseexception;
Import java. Text. simpledateformat;
Import java. util. locale;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;

Public class validatorstrategy1 implements validatorinterface
{

/**
* <PRE> time conversion class, which converts the string input by the user to the date type </PRE>
* @ Param s the meeting time that the user inputs at the UI Layer.
* @ Return tempdate the converted date type
* @ Throws parseexception
*/

Public java. util. Date convertodate (string s) throws parseexception
{
Locale locale = locale. us;
Java. util. Date tempdate = NULL;
Java. Text. dateformat df = new simpledateformat ("yyyy-mm-dd hh: mm ",
Locale );
Tempdate = DF. parse (s );
Return tempdate;
}

/**
* <PRE> use a regular expression to determine whether the string entered by the user complies with the yyyy-mm-dd hh: Mm format </PRE>
* @ Param s the string entered by the user
* @ Return whether isvalidatetime is a valid input
*/
Public Boolean isvalidatetime (string S)
{
Boolean isvalidatetime;
Pattern P = NULL; // Regular Expression
Matcher M = NULL;
P =

Pattern. Compile ("^ (1 [6-9] | [2-9] \ D) \ D {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ D) \ D {2 })-

(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ D) \ D {2})-0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ D

) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) (\ s) + (20 | 21 | 22 | 23 | [0-1]? \ D): [0-5]? \ D $"

);
M = P. matcher (s );
Isvalidatetime = M. Matches ();
Return isvalidatetime;
}

}
It is worth noting that the above regular expression can be used to determine whether the input string conforms to the format of yyyy-mm-dd hh: Mm,

Implementation file of Policy 2
Import java. Text. parseexception;
Import java. Text. simpledateformat;
Import java. util. locale;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;

Public class validatorstrategy2 implements validatorinterface
{

/**
* <PRE> time conversion class, which converts the string input by the user to the date type </PRE>
* @ Param s the meeting time that the user inputs at the UI Layer.
* @ Return tempdate the converted date type
* @ Throws parseexception
*/

Public java. util. Date convertodate (string s) throws parseexception
{
Locale locale = locale. us;
Java. util. Date tempdate = NULL;
Java. Text. dateformat df = new simpledateformat ("yyyy-mm-dd", locale );
Tempdate = DF. parse (s );
Return tempdate;
}

/**
* <PRE> use a regular expression to determine whether the string entered by the user complies with the yyyy-mm-dd format </PRE>
* @ Param s the string entered by the user
* @ Return whether isvalidatetime is a valid input
*/
Public Boolean isvalidatetime (string S)
{
Boolean isvalidatetime;
Pattern P = NULL; // Regular Expression
Matcher M = NULL;
P =

Pattern. Compile ("^ (1 [6-9] | [2-9] \ D) \ D {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ D) \ D {2 })-

(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ D) \ D {2})-0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ D

) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) $ ");
M = P. matcher (s );
Isvalidatetime = M. Matches ();
Return isvalidatetime;
}

}

 

Validator uses the policy mode class validator. Java
Mport java. Text. parseexception;

Public class validator
{
Private validatorinterface validator;

Public validator (validatorinterface validator)
{
This. validator = validator;
}

Public Boolean getvalidator (string S)
{
Return validator. isvalidatetime (s );
}

/**
* <PRE> time conversion class, which converts the string input by the user to the date type </PRE>
* @ Param s the meeting time that the user inputs at the UI Layer.
* @ Return tempdate the converted date type
* @ Throws parseexception
*/
Public java. util. Date convertodate (string s) throws parseexception
{
Return validator. convertodate (s );
}

}

Example of frontend policy Mode
// Use the yyyy-mm-dd hh: Mm policy mode to verify the date
Validatorinterface V1 = new validatorstrategy1 ();
Validator strategy = new validator (V1 );

// Determine whether the input start date is valid
If (! Strategy. getvalidator (ST )){
Joptionpane. showmessagedialog (this, "the input start date is invalid", "prompt ",
Joptionpane. plain_message );
Return;
}

You can also design another packaging class and encapsulate it in the facade mode.
/**
* <PRE> validtime class, which uses the facade mode to encapsulate the date format verification function </PRE>
* @ Version 1.0, 2006-01
* @ Author jackyrong
*/
Public class validtime
{
Private Static Boolean instance_flag = false;
Private validtime ()
{
}

Public static validtime getvalidtime ()
{
If (! Instance_flag ){
Return new validtime ();
}
Else {
Return NULL;
}

}

/**
* <PRE> determines whether a string that represents a date matches the relevant format </PRE>
* <PRE> use the policy mode to verify whether two specified date formats are met: yyyy-mm-dd Or yyyy-mm-dd hh: mm </PRE>
* @ Param s the string entered by the user
* @ Return isvalidtime is the valid date input format
*/

Public Boolean isvalidtime (string S)
{
Boolean isvalidtime = false;
// Use the policy mode of yyyy-mm-dd hh: mm
Validatorinterface V1 = new validatorstrategy1 ();
Validator strategy1 = new validator (V1 );

// Use the policy mode of yyyy-mm-dd hh: mm: Ss.
Validatorinterface v2 = new validatorstrategy2 ();
Validator strategy2 = new validator (V2 );

// If the input date string meets one of the two formats, it is recognized as the correct format
If (strategy1.getvalidator (s) | strategy2.getvalidator (s )){
Isvalidtime = true;
}
Return isvalidtime;
}

}

The above code can be used to determine whether user input has Multiple Preset inputs. The front-end call can do this.
// Determine whether the input start date is valid
If (! Validtime. getvalidtime (). isvalidtime (ST )){
Joptionpane. showmessagedialog (this, "the input start date is invalid", "prompt ",
Joptionpane. plain_message );
Return;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.