I have a function in which I need to set a string to the following format : #####-####-## and padded with leading zeroes.
eg:
1234567
will become 00001-2345-67
98765432109
will become 98765-4321-09
I'm using MaskFormatter and my impression from the documentation was that A
represented any letter or digit, and that setPlaceHolderCharacter
would pad the string.
I know technically it's for use with text fields but had hoped I could use it in a back-end function, if not is there a different component i should be using?
I have this code below but it throws an exception java.text.ParseException: Invalid character: 1
when it hits mask.valueToString(ndc)
public class NdcFormatter implements MapFunction<Purchase, Purchase> {
private final String maskFormat = "AAAAA-AAAA-AA";
private final Character placeholder = '0';
@Override
public Purchase map(Purchase purchase) throws ParseException {
if(purchase != null){
String ndc = purchase.getNdc();
MaskFormatter mask = new MaskFormatter(maskFormat);
mask.setPlaceholderCharacter(placeholder);
String result = mask.valueToString(ndc);
purchase.setNdc(result);
}
return purchase;
}
}