1 package net.sf.layoutParser.typeHandler;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6
7 import net.sf.layoutParser.exception.ExceptionKey;
8 import net.sf.layoutParser.processor.MalformedInputException;
9 import net.sf.layoutParser.processor.MalformedOutputException;
10
11
12
13
14
15
16
17
18
19 public class DateTypeHandler implements TypeHandler<Date> {
20
21 public String format(Date data, String format, String filler, int size) throws MalformedOutputException {
22 if(format == null){
23 throw new MalformedOutputException( ExceptionKey.PLUGIN_NO_FORMAT, new Object[]{data, Date.class});
24 }
25
26 SimpleDateFormat dateFormat = new SimpleDateFormat( format );
27 return dateFormat.format(data);
28 }
29
30 public Date parse(String dados, String format, String filler) throws MalformedInputException {
31 SimpleDateFormat dtFormater = new SimpleDateFormat(format);
32
33 if(hasOnlyFillers(dados, filler)){
34 return null;
35 }
36
37 try {
38 return dtFormater.parse(dados);
39 } catch (ParseException e) {
40 throw new MalformedInputException(e);
41 }
42 }
43
44 private boolean hasOnlyFillers(String dados, String filler){
45 for (char a : dados.toCharArray()) {
46 String letter = Character.toString(a);
47 if(!letter.equals(filler)){
48 return false;
49 }
50 }
51
52 return true;
53 }
54 }