001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.scripting;
013
014import java.text.SimpleDateFormat;
015import java.util.Calendar;
016import java.util.Date;
017
018/**
019 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
020 * @author <a href="mailto:tm@nuxeo.com">Thierry Martins</a>
021 */
022public class DateWrapper {
023
024    protected final long timestamp;
025
026    protected final Calendar date;
027
028    public DateWrapper() {
029        date = Calendar.getInstance();
030        timestamp = date.getTimeInMillis();
031    }
032
033    public DateWrapper(Calendar date) {
034        this.date = date;
035        timestamp = date.getTimeInMillis();
036    }
037
038    public DateWrapper(Date date) {
039        this(date.getTime());
040    }
041
042    public DateWrapper(long date) {
043        timestamp = date;
044        this.date = Calendar.getInstance();
045        this.date.setTimeInMillis(timestamp);
046    }
047
048    public DateWrapper months(int months) {
049        return dateWrapper(Calendar.MONTH, months);
050    }
051
052    public DateWrapper days(int days) {
053        return dateWrapper(Calendar.DAY_OF_MONTH, days);
054    }
055
056    public DateWrapper years(int years) {
057        return dateWrapper(Calendar.YEAR, years);
058    }
059
060    /**
061     * @since 5.7
062     */
063    protected DateWrapper dateWrapper(int unit, int value) {
064        Calendar calendar = (Calendar) date.clone();
065        calendar.add(unit, value);
066        return new DateWrapper(calendar);
067    }
068
069    public DateWrapper seconds(int seconds) {
070        return dateWrapper(Calendar.SECOND, seconds);
071    }
072
073    public DateWrapper weeks(int weeks) {
074        return dateWrapper(Calendar.WEEK_OF_MONTH, weeks);
075    }
076
077    public Calendar getCalendar() {
078        return date;
079    }
080
081    public Date getDate() {
082        return new Date(timestamp);
083    }
084
085    public long getTime() {
086        return timestamp;
087    }
088
089    public int getYear() {
090        return getCalendar().get(Calendar.YEAR);
091    }
092
093    public int getMonth() {
094        return getCalendar().get(Calendar.MONTH);
095    }
096
097    public int getDay() {
098        return getCalendar().get(Calendar.DAY_OF_MONTH);
099    }
100
101    public int getMinute() {
102        return getCalendar().get(Calendar.MINUTE);
103    }
104
105    public int getHour() {
106        return getCalendar().get(Calendar.HOUR);
107    }
108
109    public int getSecond() {
110        return getCalendar().get(Calendar.SECOND);
111    }
112
113    public int getWeek() {
114        return getCalendar().get(Calendar.WEEK_OF_YEAR);
115    }
116
117    public String format(String format) {
118        return new SimpleDateFormat(format).format(date.getTime());
119    }
120
121    @Override
122    public String toString() {
123        return toQueryString();
124    }
125
126    public String toQueryString() {
127        return new SimpleDateFormat("'TIMESTAMP' ''yyyy-MM-dd HH:mm:ss.SSS''").format(getDate());
128    }
129
130    public static void main(String[] args) {
131        DateWrapper d = new DateWrapper();
132        System.out.println(d);
133        d.months(2);
134        System.out.println(d);
135        d.months(-2);
136        System.out.println(d);
137        d.months(-2);
138        System.out.println(d);
139        d.months(-1);
140        System.out.println(d);
141
142        d.years(-2);
143        System.out.println(d);
144
145        d.years(4);
146        System.out.println(d);
147
148        d.weeks(2);
149        System.out.println(d);
150
151        d.weeks(-1);
152        System.out.println(d);
153
154    }
155}