Strict date pattern for DateTextField in Wicket - wicket

I have a DateTextField component in my application and I want the input of date using a predefined pattern. The pattern that I need is "yyyy-MM-dd". I created the DateTextField using the following code.
DateTextField dtf_ExpiryDate = DateTextField.forDatePattern("ExpDate", "yyyy-MM-dd");
dtf_ExpiryDate.add(new DatePicker());
It helps to prevent date input in most of other formats. But it accepts input in dd-MM-yyyy format and converts it into some weird format. For example, 12-06-2013 is automatically converted to 0012-06-20.
Is there a way to throw an error when the date is given in dd-MM-yyyy ?
Javadocs of DateTextField says that the conversion is done internally using the Joda time parser. Is there a way to add more constraints ?
I really don't want the data to be captured as String and add a StringValidator to check if it confirms to the pattern using Regex.
Thank you

As far as I know, the answer is NO. DateTextField uses DatePattern or DateStyles internally and it is not possible to enforce stricter rules. If you don't want to use Javascript (custom) or JQuery, you have to use a REGEX to validate the input.
Have a look at here. Use the StrictPatternDateConverter class shown below to enforce a strict validation for the input.

Yep! StringRegExp is a good solution. But another solution could be the use of jqWicket. It provides a MaskedInputBehavior. Take a look at: http://code.google.com/p/jqwicket/wiki/MaskedInputExample

Related

How to import and use StringUtils in tMap using Talend 3.6

I understand the library http://commons.apache.org/proper/commons-lang/javadocs/api-3.4/index.html provides a number of string functions including StringUtils.capitalize
In Talend OpenStudio 6.3 I have added the tLibraryLoad and in basic settings selected commons-lang3-3.4.jar In advanced settings I have import org.apache.commons.lang3.StringUtils.*;
in my tMap I have StringUtils.Capitalize(row20.Forename) assigned to a variable but I get the error The method Capitalize(String) is undefined for the type StringUtils
On TalendExchange there is a StringUtils available but its only for 6.2 and lower.
Whats the best and most reliable way to get access to additional string handling tools like Capitalize which converts a string like MONKEY to Monkey
It seems that talend uses its own StringUtils library (routines.system), then when specifying "StringUtils", Talend does not recognize your importn and still uses its own class.
In tMap, try org.apache.commons.lang3.StringUtils.capitalize(row20.Forename) instead
You almost got the correct set-up in your tLibraryload but you may try to configure it as illustrated below:
tLibraryload set-up
Tmap
Result
I think you should try StringUtils.Capitalize(row20.Forename)
You can use StringHandling.UPCASE(row20.Forename) to convert to uppercase.
In tMap, click on "..." just where you place the expression to fill the output fields. It oppens the expression builder. Search for "StringHandling" in the categories column, then click on UPCASE and complete the expression proposed as an example by TOS.

String to datetime conversion error in Powershell v5

I have a problem with 2 lines of code, which don't work as I expect. The point is that the DateTime object is being converted to string and back to DateTime, using default conversion, without explicit format specification.
$timeString = [DateTime]::Now.ToString() # contains 17.01.2017 20:01:30
$time = [DateTime]$timeString # PS blows with error
So, basically, it uses the default date format to format the string, but then it seem to use some other format to parse it back. The following line of code will work, however:
$otherTime = [DateTime]"01/17/2017 20:01:30" # will get the initial date
Could someone point me to proper documentation on the matter of types conversion, and why in this case it would use different formats to convert data back and forth?
Thanks in advance.
Parsing dates is always a nightmare. Especially if you live in the tiny part of the world that is called 'outside US' :)
In general formatting and parsing dates in .NET (and many other things as string comparison) are controlled by culture settings. In some cases then default behavior is to use the current culture settings. Convert.ToDateTime is one of them. If you take a look into the documentation (Convert.ToDateTime Method (String)) it says:
If value is not null, the return value is the result of invoking the
DateTime.Parse method on value using the formatting information in a
DateTimeFormatInfo object that is initialized for the current culture.
The value argument must contain the representation of a date and time
in one of the formats described in the DateTimeFormatInfo topic.
That's why it converts from your localized date string. In other cases the default behavior is to use the 'Invariant Culture' setting which usually means 'US settings'. Most of the methods are overloaded and can take a parameter that specifies the culture that should use, but it requires a little search into the .NET documentation.
As a rule of thumb: Don't use strings that are localized if they are not going to be shown to the end user. Always try to find the 'Invariant Culture' variant of the method, and use it for formatting and parsing of the strings. It will save you from many headaches.
You're implicitly calling Convert.ToDateTime(String), but this method's valid formats are hardcoded (and don't appear to be listed). From your output date format, I see that you're likely not in the US, which is probably what most of the formats are centered towards.
Instead, you can explicitly use Convert.ToDateTime(String, IFormatProvider) to tell it which culture format provider you want.
[Convert]::ToDateTime($timeString, [System.Globalization.DateTimeFormatInfo]::CurrentInfo)
I'm on a US system, so I'm not entirely certain if this will work yet.
You can also use [DateTime]::TryParse() or [DateTime]::TryParseExact() to explicitly specify the format(s) you want.

Do I need to provide an IFormatProvider when converting a date to a string with a specific format?

If I'm using this:
DateTime.Now.Date.ToString("yyyy-MM-dd")
FXCop complains that I'm violating CA1305 and says that I should provider an IFormatProvider. Do I need to? I'm asking for the date in a specific format anyway (which is the format I'm expected to put it into the XML as).
Would providing a format provider make any difference? Might it actually produce the wrong results in this case?
Why don't you want to specify the format provider?
If it is just laziness then I can recommend defining two snippets. ic for CultureInfo.InvariantCulture and cc for CultureInfo.CurrentCulture.
Never assume anything about how conversion to string works with the default culture. Not everyone in the world uses the gregorian calendar. Some day you customer might hire a contractor with a computer with another calendar as default and then you are not generating correct XML. Explain then to your customer that you didn't want to follow the FxCop recommendation.
Best thing would be if .Net included a Xml Culture. Then you could just do
DateTime.Today.ToString("d", CultureInfo.Xml)
For some reason Microsoft choose to create a separate class instead XmlConvert. The class has been there since .Net 1.0.
XmlConvert.ToString(DateTime.Today, "yyyy-MM-dd")
will always create a correct Xml date.
Not sure if it is bug or intended behaviour but XmlConvert.ToString(DateTime.Today, "d") will not create a valid Xml date.
so after a bit more research it seems that in my instance it doesn't make any difference, but in the general case months might be displayed in a specific locale.
More details here:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Zend_Validate good strategy to avoid repetition of code

I'm am currently building two custom validators that extends Zend_Validate_Abstract which are named respectively Lib_Validate_TimeAfter and Lib_Validate_TimeBetween. The names a pretty straight forward, the first one is used to test if a date/datetime/time comes after an other one and the second is used to test if a date/datetime/time comes between two other date/datetime/time.
Both of those validators would rely on the same method named _buildDate($value) which take a value in the form of a datestamp, a hourstamp(either hh:mm or hh:mm:ss), a timestamp or an ISO_8601 timestamp and convert it in a usable date format.
Since I dont want to repeat myself and copy/paste the method in both of my validator, I was looking for the best way to do it.
The avenues I am looking at right now would be to develop somekind of class helper that my validators would be able to use (kind of messy way of doing things since it add unessesary dependencies) or I could add an other layer of abstraction by building an other validator that validate date/datetime/time and then extend my two validators on that since I could share the method _buildDate($value), but then I don't think I would really need the validator.
So, what would be a good way (I am not really looking for the "Way of the gods" of doing thing) to structure that kind of code to avoid repetition (DRY)?
You might want to build one validator instead of two, where you can pass in a dateBefore, dateAfter which are both optional. If you pass only dateBefore, your $value will be valid if it is after that date, if you pass in both, it will have to be between them and if you pass in only dateAfter, the value will have to be before that date.
This would be flexible, clear, generic, less code and even cover one more case.
What about a class Lib_Validate_Common which extends Zend_Validate_Abstract which has your common method. And Lib_Validate_TimeAfter and Lib_Validate_TimeBetween extends Lib_Validate_Common .

How to write a string starting with '=' to a cell using Spreadsheet::WriteExcel

I'm using the Perl package Spreadsheet::WriteExcel to write an Excel file. I want to write a string that starts with the equal sign, "=ABC()", to a cell.
$ws->write('A1', '=ABC()');
But I got an error message of
Unknown function ABC() in formula
Can someone advise?
Use the write_string method directly instead of using write:
$ws->write_string('A1', '=ABC()');
Spreadsheet::WriteExcel's write method is a convenience method that guesses what kind of data you're trying to store. If it guesses wrong, you should use one of the type-specific methods.
#Cjm already provided the best answer. Still, I'd like to remember that's possible also to format any cell as text and type whatever you want, that won't be interpreted.