Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / date / TimeFunction.java @ 43987

History | View | Annotate | Download (1.38 KB)

1 43987 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.date;
2 43512 jjdelcerro
3 43987 jjdelcerro
import java.text.ParseException;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6 43512 jjdelcerro
import org.apache.commons.lang3.Range;
7 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
8 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
9
10 43987 jjdelcerro
public class TimeFunction extends AbstractFunction {
11 43512 jjdelcerro
12 43987 jjdelcerro
    public TimeFunction() {
13
        super("Date", "TIME",Range.between(1,2),
14
            "Returns a time from the arguments",
15
            "TIME({{time}}, format)",
16 43983 jjdelcerro
            new String[]{
17 43987 jjdelcerro
                "time - a string with a time",
18
                "format - Optional. Format to use to parse the time"
19 43983 jjdelcerro
            },
20 43987 jjdelcerro
            "Time"
21 43983 jjdelcerro
        );
22 43512 jjdelcerro
    }
23
24
    @Override
25 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) {
26 43987 jjdelcerro
        String date = getStr(args, 0);
27
        String format = null;
28
        if( args.length==2 ) {
29
            format = getStr(args, 1);
30 43512 jjdelcerro
        }
31 43987 jjdelcerro
        SimpleDateFormat df = new SimpleDateFormat();
32
        if( format != null ) {
33
            df.applyPattern(format);
34 43512 jjdelcerro
        }
35 43987 jjdelcerro
        try {
36
            Date x = df.parse(date);
37
            x.setDate(0);
38
            x.setMonth(0);
39
            x.setYear(0);
40
            return x;
41
        } catch (ParseException ex) {
42
            throw new RuntimeException("Can't parse date value '"+date+"' with format '"+format==null?"":format+"'", ex);
43 43512 jjdelcerro
        }
44 43987 jjdelcerro
45 43512 jjdelcerro
    }
46
47
48
}