001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.util;
020
021import java.text.SimpleDateFormat;
022import java.util.Calendar;
023import java.util.Date;
024
025/**
026 * Date utilities class.
027 * 
028 * @author Stephane
029 */
030public class DateUtil
031{
032    public static final long DAY_IN_MS = 1000 * 60 * 60 * 24;
033
034    /**
035     * Returns elapsed milli second from 01/01/1970 (same as {@link System#currentTimeMillis()})
036     */
037    public static long getTime()
038    {
039        return System.currentTimeMillis();
040    }
041
042    /**
043     * Keep only the day part information from the given time (elapsed milli second from
044     * 01/01/1970).
045     * 
046     * @see System#currentTimeMillis
047     */
048    public static long keepDay(long time)
049    {
050        return (time / DAY_IN_MS) * DAY_IN_MS;
051    }
052
053    /**
054     * Keep only the time information (hour, minute, second and milli second) from the given time
055     * (elapsed milli second from 01/01/1970).
056     * 
057     * @see System#currentTimeMillis
058     */
059    public static long keepTime(long time)
060    {
061        return time % DAY_IN_MS;
062    }
063
064    /**
065     * Return elapsed day from 01/01/1970
066     * 
067     * @see System#currentTimeMillis
068     */
069    public static long getDay()
070    {
071        return getTime() / DAY_IN_MS;
072    }
073
074    /**
075     * Return current date.
076     */
077    public static Date now()
078    {
079        return Calendar.getInstance().getTime();
080    }
081
082    /**
083     * Return current date (String format).<br>
084     * 
085     * @param format
086     *        define the wanted format.<br>
087     *        Ex :<br>
088     *        DateUtil.now("dd MMMMM yyyy");<br>
089     *        DateUtil.now("yyyyMMdd");<br>
090     *        DateUtil.now("MM/dd/yy");<br>
091     *        DateUtil.now("yyyy.MM.dd G 'at' hh:mm:ss z");<br>
092     *        DateUtil.now("H:mm:ss:SSS");<br>
093     *        DateUtil.now("yyyy.MMMMM.dd GGG hh:mm aaa");<br>
094     */
095    public static String now(String format)
096    {
097        return new SimpleDateFormat(format).format(now());
098    }
099
100    /**
101     * Return the specified date in String format.<br>
102     * 
103     * @param format
104     *        define the wanted format.<br>
105     *        Ex :<br>
106     *        DateUtil.now("dd MMMMM yyyy");<br>
107     *        DateUtil.now("yyyyMMdd");<br>
108     *        DateUtil.now("MM/dd/yy");<br>
109     *        DateUtil.now("yyyy.MM.dd G 'at' hh:mm:ss z");<br>
110     *        DateUtil.now("H:mm:ss:SSS");<br>
111     *        DateUtil.now("yyyy.MMMMM.dd GGG hh:mm aaa");<br>
112     */
113    public static String format(String format, Date date)
114    {
115        return new SimpleDateFormat(format).format(date);
116    }
117    
118    /**
119     * Returns given time in ms in in international time String format.
120     * 
121     * @param valueInMs
122     *        : value in milliseconds
123     * @return <b>Example:</b> "2:21:18.345" for 2h21mn, "1.543" for 1 second and 543 ms
124     */
125    public static String getTimeAsString(double valueInMs)
126    {
127        String result = "";
128        double v = valueInMs;
129
130        if (v >= 3600000d)
131        {
132            result += (int) (v / 3600000d);
133            v %= 3600000d;
134        }
135        if (v >= 60000d)
136        {
137            if (StringUtil.isEmpty(result)) result += ":";
138            result += (int) (v / 60000d);
139            v %= 60000d;
140        }
141        if (v >= 1000d)
142        {
143            if (StringUtil.isEmpty(result)) result += ":";
144            result += (int) (v / 1000d);
145            v %= 1000d;
146        }
147        if (v != 0d)
148        {
149            if (StringUtil.isEmpty(result)) result += ".";
150            result += (int) v;
151        }
152
153        return result;
154    }
155}