DateUtils.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:12k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DateUtils.java,v 1.4 2005/10/10 18:02:45 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.calendar;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. /**
  25.  * @author Scott Violet
  26.  * @version  $Revision: 1.4 $
  27.  */
  28. public class DateUtils {
  29.     private static Calendar CALENDAR = Calendar.getInstance();
  30.     /**
  31.      * Returns the last millisecond of the specified date.
  32.      *
  33.      * @param date Date to calculate end of day from
  34.      * @return Last millisecond of <code>date</code>
  35.      */
  36.     public static Date endOfDay(Date date) {
  37.         Calendar calendar = CALENDAR;
  38.         synchronized(calendar) {
  39.             calendar.setTime(date);
  40.             calendar.set(Calendar.HOUR_OF_DAY, 23);
  41.             calendar.set(Calendar.MILLISECOND, 999);
  42.             calendar.set(Calendar.SECOND, 59);
  43.             calendar.set(Calendar.MINUTE, 59);
  44.             return calendar.getTime();
  45.         }
  46.     }
  47.     /**
  48.      * Returns a new Date with the hours, milliseconds, seconds and minutes
  49.      * set to 0.
  50.      *
  51.      * @param date Date used in calculating start of day
  52.      * @return Start of <code>date</code>
  53.      */
  54.     public static Date startOfDay(Date date) {
  55.         Calendar calendar = CALENDAR;
  56.         synchronized(calendar) {
  57.             calendar.setTime(date);
  58.             calendar.set(Calendar.HOUR_OF_DAY, 0);
  59.             calendar.set(Calendar.MILLISECOND, 0);
  60.             calendar.set(Calendar.SECOND, 0);
  61.             calendar.set(Calendar.MINUTE, 0);
  62.             return calendar.getTime();
  63.         }
  64.     }
  65.      /**
  66.      * Returns day in millis with the hours, milliseconds, seconds and minutes
  67.      * set to 0.
  68.      *
  69.      * @param date long used in calculating start of day
  70.      * @return Start of <code>date</code>
  71.      */
  72.     public static long startOfDayInMillis(long date) {
  73.         Calendar calendar = CALENDAR;
  74.         synchronized(calendar) {
  75.             calendar.setTimeInMillis(date);
  76.             calendar.set(Calendar.HOUR_OF_DAY, 0);
  77.             calendar.set(Calendar.MILLISECOND, 0);
  78.             calendar.set(Calendar.SECOND, 0);
  79.             calendar.set(Calendar.MINUTE, 0);
  80.             return calendar.getTimeInMillis();
  81.         }
  82.     }
  83.     /**
  84.      * Returns the last millisecond of the specified date.
  85.      *
  86.      * @param date long to calculate end of day from
  87.      * @return Last millisecond of <code>date</code>
  88.      */
  89.     public static long endOfDayInMillis(long date) {
  90.         Calendar calendar = CALENDAR;
  91.         synchronized(calendar) {
  92.             calendar.setTimeInMillis(date);
  93.             calendar.set(Calendar.HOUR_OF_DAY, 23);
  94.             calendar.set(Calendar.MILLISECOND, 999);
  95.             calendar.set(Calendar.SECOND, 59);
  96.             calendar.set(Calendar.MINUTE, 59);
  97.             return calendar.getTimeInMillis();
  98.         }
  99.     }
  100.     /**
  101.      * Returns the day after <code>date</code>.
  102.      *
  103.      * @param date Date used in calculating next day
  104.      * @return Day after <code>date</code>.
  105.      */
  106.     public static Date nextDay(Date date) {
  107.         return new Date(addDays(date.getTime(), 1));
  108.     }
  109.     /**
  110.      * Adds <code>amount</code> days to <code>time</code> and returns
  111.      * the resulting time.
  112.      *
  113.      * @param time Base time
  114.      * @param amount Amount of increment.
  115.      * 
  116.      * @return the <var>time</var> + <var>amount</var> days
  117.      */
  118.     public static long addDays(long time, int amount) {
  119.         Calendar calendar = CALENDAR;
  120.         synchronized(calendar) {
  121.             calendar.setTimeInMillis(time);
  122.             calendar.add(Calendar.DAY_OF_MONTH, amount);
  123.             return calendar.getTimeInMillis();
  124.         }
  125.     }
  126.     /**
  127.      * Returns the day after <code>date</code>.
  128.      *
  129.      * @param date Date used in calculating next day
  130.      * @return Day after <code>date</code>.
  131.      */
  132.     public static long nextDay(long date) {
  133.         return addDays(date, 1);
  134.     }
  135.     /**
  136.      * Returns the week after <code>date</code>.
  137.      *
  138.      * @param date Date used in calculating next week
  139.      * @return week after <code>date</code>.
  140.      */
  141.     public static long nextWeek(long date) {
  142.         return addDays(date, 7);
  143.     }
  144.     /**
  145.      * Returns the number of days difference between <code>t1</code> and
  146.      * <code>t2</code>.
  147.      *
  148.      * @param t1 Time 1
  149.      * @param t2 Time 2
  150.      * @param checkOverflow indicates whether to check for overflow
  151.      * @return Number of days between <code>start</code> and <code>end</code>
  152.      */
  153.     public static int getDaysDiff(long t1, long t2, boolean checkOverflow) {
  154.         if (t1 > t2) {
  155.             long tmp = t1;
  156.             t1 = t2;
  157.             t2 = tmp;
  158.         }
  159.         Calendar calendar = CALENDAR;
  160.         synchronized(calendar) {
  161.             calendar.setTimeInMillis(t1);
  162.             int delta = 0;
  163.             while (calendar.getTimeInMillis() < t2) {
  164.                 calendar.add(Calendar.DAY_OF_MONTH, 1);
  165.                 delta++;
  166.             }
  167.             if (checkOverflow && (calendar.getTimeInMillis() > t2)) {
  168.                 delta--;
  169.             }
  170.             return delta;
  171.         }
  172.     }
  173.    /**
  174.      * Returns the number of days difference between <code>t1</code> and
  175.      * <code>t2</code>.
  176.      *
  177.      * @param t1 Time 1
  178.      * @param t2 Time 2
  179.      * @return Number of days between <code>start</code> and <code>end</code>
  180.      */
  181.       public static int getDaysDiff(long t1, long t2) {
  182.        return  getDaysDiff(t1, t2, true);
  183.     }
  184.     /**
  185.      * Check, whether the date passed in is the first day of the year.
  186.      *
  187.      * @param date date to check in millis
  188.      * @return <code>true</code> if <var>date</var> corresponds to the first
  189.      *         day of a year
  190.      * @see Date#getTime() 
  191.      */
  192.     public static boolean isFirstOfYear(long date) {
  193.         boolean ret = false;
  194.         Calendar calendar = CALENDAR;
  195.         synchronized(calendar) {
  196.             calendar.setTimeInMillis(date);
  197.             int currentYear = calendar.get(Calendar.YEAR);
  198.             // Check yesterday
  199.             calendar.add(Calendar.DATE,-1);
  200.             int yesterdayYear = calendar.get(Calendar.YEAR);
  201.             ret = (currentYear != yesterdayYear);
  202.         }
  203.         return ret;
  204.     }
  205.     /**
  206.      * Check, whether the date passed in is the first day of the month.
  207.      *
  208.      * @param date date to check in millis
  209.      * @return <code>true</code> if <var>date</var> corresponds to the first
  210.      *         day of a month
  211.      * @see Date#getTime() 
  212.      */
  213.     public static boolean isFirstOfMonth(long date) {
  214.         boolean ret = false;
  215.         Calendar calendar = CALENDAR;
  216.         synchronized(calendar) {
  217.             calendar.setTimeInMillis(date);
  218.             int currentMonth = calendar.get(Calendar.MONTH);
  219.             // Check yesterday
  220.             calendar.add(Calendar.DATE,-1);
  221.             int yesterdayMonth = calendar.get(Calendar.MONTH);
  222.             ret =  (currentMonth != yesterdayMonth);
  223.         }
  224.         return ret;     
  225.     }
  226.     /**
  227.      * Returns the day before <code>date</code>.
  228.      *
  229.      * @param date Date used in calculating previous day
  230.      * @return Day before <code>date</code>.
  231.      */
  232.     public static long previousDay(long date) {
  233.         return addDays(date, -1);
  234.     }
  235.     /**
  236.      * Returns the week before <code>date</code>.
  237.      *
  238.      * @param date Date used in calculating previous week
  239.      * @return week before <code>date</code>.
  240.      */
  241.     public static long previousWeek(long date) {
  242.         return addDays(date, -7);
  243.     }
  244.     /**
  245.      * Returns the first day before <code>date</code> that has the
  246.      * day of week matching <code>startOfWeek</code>.  For example, if you
  247.      * want to find the previous monday relative to <code>date</code> you
  248.      * would call <code>getPreviousDay(date, Calendar.MONDAY)</code>.
  249.      *
  250.      * @param date Base date
  251.      * @param startOfWeek Calendar constant correspoding to start of week.
  252.      * @return start of week, return value will have 0 hours, 0 minutes,
  253.      *         0 seconds and 0 ms.
  254.      * 
  255.      */
  256.     public static long getPreviousDay(long date, int startOfWeek) {
  257.         return getDay(date, startOfWeek, -1);
  258.     }
  259.     /**
  260.      * Returns the first day after <code>date</code> that has the
  261.      * day of week matching <code>startOfWeek</code>.  For example, if you
  262.      * want to find the next monday relative to <code>date</code> you
  263.      * would call <code>getPreviousDay(date, Calendar.MONDAY)</code>.
  264.      *
  265.      * @param date Base date
  266.      * @param startOfWeek Calendar constant correspoding to start of week.
  267.      * @return start of week, return value will have 0 hours, 0 minutes,
  268.      *         0 seconds and 0 ms.
  269.      * 
  270.      */
  271.     public static long getNextDay(long date, int startOfWeek) {
  272.         return getDay(date, startOfWeek, 1);
  273.     }
  274.     private static long getDay(long date, int startOfWeek, int increment) {
  275.         Calendar calendar = CALENDAR;
  276.         synchronized(calendar) {
  277.             calendar.setTimeInMillis(date);
  278.             int day = calendar.get(Calendar.DAY_OF_WEEK);
  279.             // Normalize the view starting date to a week starting day
  280.             while (day != startOfWeek) {
  281.                 calendar.add(Calendar.DATE, increment);
  282.                 day = calendar.get(Calendar.DAY_OF_WEEK);
  283.             }
  284.             return startOfDayInMillis(calendar.getTimeInMillis());
  285.         }
  286.     }
  287.     /**
  288.      * Returns the previous month.
  289.      * 
  290.      * @param date Base date
  291.      * @return previous month
  292.      */
  293.     public static long getPreviousMonth(long date) {
  294.         return incrementMonth(date, -1);
  295.     }
  296.     /**
  297.      * Returns the next month.
  298.      * 
  299.      * @param date Base date
  300.      * @return next month
  301.      */
  302.     public static long getNextMonth(long date) {
  303.         return incrementMonth(date, 1);
  304.     }
  305.     private static long incrementMonth(long date, int increment) {
  306.         Calendar calendar = CALENDAR;
  307.         synchronized(calendar) {
  308.             calendar.setTimeInMillis(date);
  309.             calendar.add(Calendar.MONTH, increment);
  310.             return calendar.getTimeInMillis();
  311.         }
  312.     }
  313.     /**
  314.      * Returns the date corresponding to the start of the month.
  315.      *
  316.      * @param date Base date
  317.      * @return Start of month.
  318.      */
  319.     public static long getStartOfMonth(long date) {
  320.         return getMonth(date, -1);
  321.     }
  322.     /**
  323.      * Returns the date corresponding to the end of the month.
  324.      *
  325.      * @param date Base date
  326.      * @return End of month.
  327.      */
  328.     public static long getEndOfMonth(long date) {
  329.         return getMonth(date, 1);
  330.     }
  331.     private static long getMonth(long date, int increment) {
  332. long result;
  333.         Calendar calendar = CALENDAR;
  334.         synchronized(calendar) {
  335.             calendar.setTimeInMillis(date);
  336.             if (increment == -1) {
  337.                 calendar.set(Calendar.DAY_OF_MONTH, 1);
  338.                 result = startOfDayInMillis(calendar.getTimeInMillis());
  339.             } else {
  340.                 calendar.add(Calendar.MONTH, 1);
  341.                 calendar.set(Calendar.DAY_OF_MONTH, 1);
  342.                 calendar.set(Calendar.HOUR_OF_DAY, 0);
  343.                 calendar.set(Calendar.MILLISECOND, 0);
  344.                 calendar.set(Calendar.SECOND, 0);
  345.                 calendar.set(Calendar.MINUTE, 0);
  346.                 calendar.add(Calendar.MILLISECOND, -1);
  347.                 result = calendar.getTimeInMillis();
  348.             }
  349.         }
  350. return result;
  351.     }
  352.     /**
  353.      * Returns the day of the week.
  354.      *
  355.      * @param date date
  356.      * @return day of week.
  357.      */
  358.     public static int getDayOfWeek(long date) {
  359.        Calendar calendar = CALENDAR;
  360.         synchronized(calendar) {
  361.             calendar.setTimeInMillis(date);
  362.             return (calendar.get(Calendar.DAY_OF_WEEK));
  363.         }
  364.     }
  365. }