⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dateutility.java

📁 采用JAVA开发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * $RCSfile: DateUtility.java,v $
 * $Revision: 1.2 $
 * $Date: 2004/01/17 14:23:40 $
 *
 * Copyright (C) 2003 ICSS, Inc. All rights reserved.
 *
 * This software is the proprietary information of ICSS, Inc.
 * Use is subject to license terms.
 */
package com.gctech.misc.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>Title: OA系统</p>
 * <p>Description: 日期常用的方法</p>
 * <p>Copyright: Copyright (c) 2002-12-5</p>
 * <p>Company: 中软远东国际</p>
 *
 * @version 1.0
 * @author  Andy
 */
public class DateUtility {

    /**
     * 比较两个日期 返回值为两个日期相差的天数
     *
     * @return int
     * @param sDate1 java.lang.String
     * @param sDate2 java.lang.String
     */
    public static int compareDate(String sDate1, String sDate2) {
        DateFormat dateFormat = DateFormat.getDateInstance();
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = dateFormat.parse(sDate1);
            date2 = dateFormat.parse(sDate2);
        }
        catch (ParseException e) {
            throw new IllegalArgumentException(e.getMessage());
        }

        long dif = 0;
        if (date2.after(date1))
            dif = (date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24;
        else
            dif = (date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24;

        return (int)dif;
    }

    /**
     * 获取当前日期字符串 格式为YYYY-MM-DD
     *
     * @return java.lang.String
     */
    public static String getCurrentDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String s = df.format(new Date());
        return s;
    }

    /**
     * 获取当前日期及时间字符串 格式为YYYY-MM-DD HH:mm:ss
     *
     * @return java.lang.String
     */
    public static String getCurrentDateTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = df.format(new Date());
        return s;
    }

    /**
     * 获取当前时间字符串 格式为HH:mm:ss
     *
     * @return java.lang.String
     */
    public static String getCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        return df.format(new Date());
    }

    /**
     * 获取当前日期中的日
     *
     * @return java.lang.String
     */
    public static String getCurrentDay() {
        String day;
        SimpleDateFormat df = new SimpleDateFormat("d");
        day = df.format(new Date());
        return day;
    }

    /**
     * 获取当前日期中的月
     *
     * @return java.lang.String
     */
    public static String getCurrentMonth() {
        String month;
        SimpleDateFormat df = new SimpleDateFormat("MM");
        month = df.format(new Date());
        return month;
    }

    /**
     * 获取当前日期中的年
     *
     * @return java.lang.String
     */
    public static String getCurrentYear() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy");
        return df.format(new Date());
    }

    /**
     *将字符串格式日期转换成long型
     * @param strDate type:YYYY-MM-DD
     * @return java.lang.Long
     */
    public static Long getLongDate(String strDate) {
        Date date = java.sql.Date.valueOf(strDate);
        Long lDate = new Long(date.getTime());
        return (lDate);
    }

    /**
     *将字符串格式日期转换成long型    *
     * @param      iType value  strDate type
     *              0          :YYYY-MM-DD
     *              1          :YYYY-MM-DD hh:mm:ss
     * @return java.lang.Long
     */
    public static Long getLongDate(String strDate, int iType) {
        Long retDate = null;
        switch (iType) {
            case 0 :
                retDate = getLongDate(strDate);
                break;
            case 1 :
                retDate = new Long(java.sql.Timestamp.valueOf(strDate).getTime());
                break;
        }
        return retDate;
    }

    /**
     *将Long格式日期转换成字符串型
     * @param lDate
     * iType value  output
     *       0     YYYY-MM-DD
     *       1     YYYY-MM-DD hh
     *       2     YYYY-MM-DD hh:ss
     *       3     YYYY-MM-DD hh:ss:mm
     * @return java.lang.String
     */

    public static String getStrDate(java.lang.Long lDate, int iType) {
        Date date = new Date(lDate.longValue());
        SimpleDateFormat simpleDateFormat = null;
        if (lDate == null) {
            return "";
        }
        switch (iType) {
            case 0 :
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                break;
            case 1 :
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
                break;
            case 2 :
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                break;
            case 3 :
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
        }

        String strDate = simpleDateFormat.format(date);
        return (strDate);
    }
    
    /**
     *将字符型日期加入指定天数(add in 2003.11.21 @author windy)
     *<p>for example:</p>
     *<p>getDate("1970-1-1",2),so this will return "1970-1-3"</p>
     * @param String aDate
     * @param int dif
     * @return java.lang.String
     */
    public static String getDate(String aDate, int dif)
    {
        java.sql.Date date = null;
        try
        {
            date = java.sql.Date.valueOf(aDate);
            System.out.println();
        }
        catch(Exception e)
        {
            System.err.println("Application log:Catch Exception in getDate()");
            System.err.println("aDate:" + aDate);
            System.err.println(e.getMessage());
            e.printStackTrace();
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(5, dif);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d");
        String s = df.format(calendar.getTime());
        return s;
    }//eof getDate(String,int)
    
    /**
     *获取字符型日期的下一个月时间(add in 2003.11.21 @author windy)
     *<p>for example:</p>
     *<p>getDateAfterMonth("1970-1-1"),so this will return "1970-2-1"</p>
     * @param String aDate
     * @return java.lang.String
     */
    public static String getDateAfterMonth(String aDate)
    {
        java.sql.Date date1 = null;
        try
        {
            date1 = java.sql.Date.valueOf(aDate);
        }
        catch(Exception e)
        {
            System.err.println("Application log:Catch Exception in getDateBeforeAMonth(String)");
            System.err.println("aDate:" + aDate);
            System.err.println(e.getMessage());
            e.printStackTrace();
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        calendar.add(2, 1);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String s = df.format(calendar.getTime());
        return s;
    }//eof getDateAfterMonth(aDate)
    
    /**
     *获取字符型日期的下一个月的指定时间(add in 2003.11.21 @author windy)
     *<p>for example:</p>
     *<p>getDateAfterMonth("1970-1-1",12),so this will return "1970-2-12"</p>
     * @param String aDate
     * @return java.lang.String
     */
    public static String getDateAfterMonth(String aDate, int n)
    {
        DateFormat dateFormat = DateFormat.getDateInstance();
        Date date1 = null;
        try
        {
            date1 = dateFormat.parse(aDate);
        }
        catch(ParseException e)
        {
            System.err.println("Application log:Catch Exception in getDateBeforeAMonth(String)");
            System.err.println("aDate:" + aDate);
            System.err.println(e.getMessage());
            e.printStackTrace();
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        calendar.add(2, 1);
        calendar.set(5, n);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String s = df.format(calendar.getTime());
        return s;
    }//eof getDateAfterMonth(String,int)
    
    /**
     *获取字符型日期的当月的最后一天(add in 2003.11.21 @author windy)
     *<p>for example:</p>
     *<p>getLastDate("1970-1-1"),so this will return 31</p>
     * @param String aDate
     * @return int 当月最后一天的号码
     */
    public static int getLastDate(String selectDate)
    {
        int dates = 0;
        Calendar calendar = Calendar.getInstance();
        try
        {
            calendar.setTime(DateFormat.getDateInstance().parse(selectDate));
        }
        catch(ParseException e) { }
        int year = calendar.get(1);
        switch(calendar.get(2) + 1)
        {
        default:
            break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -