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 / TimestampFunction.java @ 44198
History | View | Annotate | Download (1.54 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 | 44198 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TIMESTAMP; |
8 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
9 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
10 | |||
11 | 43987 | jjdelcerro | public class TimestampFunction extends AbstractFunction { |
12 | 43512 | jjdelcerro | |
13 | 43987 | jjdelcerro | public TimestampFunction() {
|
14 | 44198 | jjdelcerro | super("Date", FUNCTION_TIMESTAMP,Range.between(1,2), |
15 | 43987 | jjdelcerro | "Returns a time stamp from the arguments",
|
16 | 44198 | jjdelcerro | FUNCTION_TIMESTAMP+"({{timestamp}}, format)",
|
17 | 43983 | jjdelcerro | new String[]{ |
18 | 43987 | jjdelcerro | "timestamp - a string with a date/time",
|
19 | "format - Optional. Format to use to parse the timestamp"
|
||
20 | 43983 | jjdelcerro | }, |
21 | 43987 | jjdelcerro | "Timestamp"
|
22 | 43983 | jjdelcerro | ); |
23 | 43512 | jjdelcerro | } |
24 | |||
25 | @Override
|
||
26 | 44009 | jjdelcerro | public boolean allowConstantFolding() { |
27 | return true; |
||
28 | } |
||
29 | |||
30 | @Override
|
||
31 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) { |
32 | 43987 | jjdelcerro | String date = getStr(args, 0); |
33 | String format = null; |
||
34 | if( args.length==2 ) { |
||
35 | format = getStr(args, 1);
|
||
36 | 43512 | jjdelcerro | } |
37 | 43987 | jjdelcerro | SimpleDateFormat df = new SimpleDateFormat(); |
38 | if( format != null ) { |
||
39 | df.applyPattern(format); |
||
40 | 43512 | jjdelcerro | } |
41 | 43987 | jjdelcerro | try {
|
42 | Date x = df.parse(date);
|
||
43 | return x;
|
||
44 | } catch (ParseException ex) { |
||
45 | throw new RuntimeException("Can't parse date value '"+date+"' with format '"+format==null?"":format+"'", ex); |
||
46 | 43512 | jjdelcerro | } |
47 | 43987 | jjdelcerro | |
48 | 43512 | jjdelcerro | } |
49 | |||
50 | |||
51 | } |