01: /*
02: * @(#)DateElement.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import java.util.Date;
12: import java.util.Calendar;
13: import pnuts.lang.PnutsFunction;
14: import pnuts.lang.Context;
15:
16: class DateElement extends PnutsFunction {
17: private int element;
18:
19: DateElement(int element, String name) {
20: super (name);
21: this .element = element;
22: }
23:
24: public boolean defined(int nargs) {
25: return nargs == 1;
26: }
27:
28: protected Object exec(Object[] args, Context context) {
29: if (args.length != 1) {
30: undefined(args, context);
31: return null;
32: }
33: Date d = (Date) args[0];
34: Calendar c = date.getCalendar(d, context);
35: if (element == Calendar.MONTH) {
36: return new Integer(c.get(element) + 1);
37: } else {
38: return new Integer(c.get(element));
39: }
40: }
41:
42: public String toString() {
43: return "function " + name + "(Date)";
44: }
45: }
|