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

View differences:

CoerceToTime.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
33
 * will use the {@link Object#toString()} method to convert the
34
 * resulting {@link String} to a Date object using the current locale default
35
 * formatter for Time through the {@link DateFormat#getTimeInstance(int)} method
36
 * and the {@link DateFormat#MEDIUM} style.
37
 * 
35
 * will use the {@link Object#toString()} method to convert the resulting
36
 * {@link String} to a Date object using the current locale default formatter
37
 * for Time through the {@link DateFormat#getTimeInstance(int)} method and the
38
 * {@link DateFormat#MEDIUM} style.
39
 *
38 40
 * @author gvSIG Team
39 41
 * @version $Id$
40 42
 */
41 43
public class CoerceToTime extends AbstractCoerceToDate {
42 44

  
43
    @Override
44
    protected DateFormat[] getFormatters(Locale locale) {
45
        return new DateFormat[] {
46
            DateFormat.getTimeInstance(DateFormat.MEDIUM, locale),
47
            DateFormat.getTimeInstance(DateFormat.SHORT, locale),
48
            DateFormat.getTimeInstance(DateFormat.LONG, locale),
49
            new SimpleDateFormat("H:mm:ss"),
50
            new SimpleDateFormat("H:mm"),
51
            new SimpleDateFormat("HHmmss") // DBF format
52
        };
45
  @Override
46
  protected DateFormat[] getFormatters(Locale locale) {
47
    return new DateFormat[]{
48
      DateFormat.getTimeInstance(DateFormat.MEDIUM, locale),
49
      DateFormat.getTimeInstance(DateFormat.SHORT, locale),
50
      DateFormat.getTimeInstance(DateFormat.LONG, locale),
51
      new SimpleDateFormat("H:mm:ss"),
52
      new SimpleDateFormat("H:mm"),
53
      new SimpleDateFormat("HHmmss") // DBF format
54
    };
55
  }
56

  
57
  @Override
58
  protected Date now() {
59
    Date d = new Date();
60
    d.setYear(0);
61
    d.setMonth(0);
62
    d.setDate(0);
63
    return d;
64
  }
65

  
66
  @Override
67
  protected String getDateType() {
68
    return "Time";
69
  }
70

  
71
  @Override
72
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
73
    if( value == null ) {
74
      return null;
53 75
    }
54
    
55
    protected Date now() {
56
        Date d = new Date();
57
        d.setYear(0);
58
        d.setMonth(0);
59
        d.setDate(0);
60
        return d;
76
    if (value instanceof Date) {
77
      Date d = (Date) value;
78
      Date n = new Date(0, 0, 0, d.getHours(), d.getMinutes(), d.getSeconds());
79
      return n;
61 80
    }
81
    return super.coerce(value, context);
82
  }
62 83

  
63
    @Override
64
    protected String getDateType() {
65
        return "Time";
66
    }
67

  
68 84
}

Also available in: Unified diff