001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.scripting;
020
021import java.text.SimpleDateFormat;
022import java.util.Calendar;
023import java.util.Date;
024
025/**
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 * @author <a href="mailto:tm@nuxeo.com">Thierry Martins</a>
028 */
029public class DateWrapper {
030
031    protected final long timestamp;
032
033    protected final Calendar date;
034
035    public DateWrapper() {
036        date = Calendar.getInstance();
037        timestamp = date.getTimeInMillis();
038    }
039
040    public DateWrapper(Calendar date) {
041        this.date = date;
042        timestamp = date.getTimeInMillis();
043    }
044
045    public DateWrapper(Date date) {
046        this(date.getTime());
047    }
048
049    public DateWrapper(long date) {
050        timestamp = date;
051        this.date = Calendar.getInstance();
052        this.date.setTimeInMillis(timestamp);
053    }
054
055    public DateWrapper months(int months) {
056        return dateWrapper(Calendar.MONTH, months);
057    }
058
059    public DateWrapper days(int days) {
060        return dateWrapper(Calendar.DAY_OF_MONTH, days);
061    }
062
063    public DateWrapper years(int years) {
064        return dateWrapper(Calendar.YEAR, years);
065    }
066
067    /**
068     * @since 5.7
069     */
070    protected DateWrapper dateWrapper(int unit, int value) {
071        Calendar calendar = (Calendar) date.clone();
072        calendar.add(unit, value);
073        return new DateWrapper(calendar);
074    }
075
076    public DateWrapper seconds(int seconds) {
077        return dateWrapper(Calendar.SECOND, seconds);
078    }
079
080    public DateWrapper weeks(int weeks) {
081        return dateWrapper(Calendar.WEEK_OF_MONTH, weeks);
082    }
083
084    public Calendar getCalendar() {
085        return date;
086    }
087
088    public Date getDate() {
089        return new Date(timestamp);
090    }
091
092    public long getTime() {
093        return timestamp;
094    }
095
096    public int getYear() {
097        return getCalendar().get(Calendar.YEAR);
098    }
099
100    public int getMonth() {
101        return getCalendar().get(Calendar.MONTH);
102    }
103
104    public int getDay() {
105        return getCalendar().get(Calendar.DAY_OF_MONTH);
106    }
107
108    public int getMinute() {
109        return getCalendar().get(Calendar.MINUTE);
110    }
111
112    public int getHour() {
113        return getCalendar().get(Calendar.HOUR);
114    }
115
116    public int getSecond() {
117        return getCalendar().get(Calendar.SECOND);
118    }
119
120    public int getWeek() {
121        return getCalendar().get(Calendar.WEEK_OF_YEAR);
122    }
123
124    public String format(String format) {
125        return new SimpleDateFormat(format).format(date.getTime());
126    }
127
128    @Override
129    public String toString() {
130        return toQueryString();
131    }
132
133    public String toQueryString() {
134        return new SimpleDateFormat("'TIMESTAMP' ''yyyy-MM-dd HH:mm:ss.SSS''").format(getDate());
135    }
136}