Revision 2080 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDate.java

View differences:

CoerceToDate.java
27 27
import java.text.SimpleDateFormat;
28 28
import java.util.Date;
29 29
import java.util.Locale;
30
import org.gvsig.tools.dataTypes.CoercionException;
31
import org.gvsig.tools.dataTypes.CoercionContext;
30 32

  
31 33
/**
32 34
 * Coerces a value to a {@link Date}. If the value is not a {@link Date}, it
......
41 43
@SuppressWarnings("UseSpecificCatch")
42 44
public class CoerceToDate extends AbstractCoerceToDate {
43 45

  
44
    @Override
45
    protected DateFormat[] getFormatters(Locale locale) {
46
        return new DateFormat[]{
47
            DateFormat.getDateInstance(DateFormat.SHORT, locale),
48
            DateFormat.getDateInstance(DateFormat.MEDIUM, locale),
49
            DateFormat.getDateInstance(DateFormat.LONG, locale),
50
            DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale),
51
            new SimpleDateFormat("yyyy-MM-dd"), // PostgreSQL format
52
            new SimpleDateFormat("yyyyMMddHHmmss"), // DBF timestamp format
53
            new SimpleDateFormat("yyyyMMdd"), // DBF date format
54
            new SimpleDateFormat("HHmmss") // DBF time format 
55
        };
56
    }
46
  @Override
47
  protected DateFormat[] getFormatters(Locale locale) {
48
    return new DateFormat[]{
49
      DateFormat.getDateInstance(DateFormat.SHORT, locale),
50
      DateFormat.getDateInstance(DateFormat.MEDIUM, locale),
51
      DateFormat.getDateInstance(DateFormat.LONG, locale),
52
      DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale),
53
      new SimpleDateFormat("yyyy-MM-dd"), // PostgreSQL format
54
      new SimpleDateFormat("yyyyMMddHHmmss"), // DBF timestamp format
55
      new SimpleDateFormat("yyyyMMdd"), // DBF date format
56
      new SimpleDateFormat("HHmmss") // DBF time format 
57
    };
58
  }
57 59

  
58
    protected Date now() {
59
        Date d = new Date();
60
        d.setHours(0);
61
        d.setMinutes(0);
62
        d.setSeconds(0);
63
        return d;
60
  @Override
61
  protected Date now() {
62
    Date d = new Date();
63
    d.setHours(0);
64
    d.setMinutes(0);
65
    d.setSeconds(0);
66
    return d;
67
  }
68

  
69
  @Override
70
  protected String getDateType() {
71
    return "Date";
72
  }
73

  
74
  @Override
75
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
76
    if( value == null ) {
77
      return null;
64 78
    }
65
    
66
    @Override
67
    protected String getDateType() {
68
        return "Date";
79
    if (value instanceof Date) {
80
      Date d = (Date) value;
81
      Date n = new Date(d.getYear(), d.getMonth(), d.getDate(), 0, 0, 0);
82
      return n;
69 83
    }
84
    return super.coerce(value, context);
85
  }
70 86

  
71 87
}

Also available in: Unified diff