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

📄 dateutil.java

📁 一个webwork+spring+ibatis的小例子
💻 JAVA
字号:
/*
 * Created on 2004-12-16 Author 孙安俊
 *
 * Copyright (c) 2003-2005 by HodeSoft
 * All rights reserved.
 */
package org.sanv.util.common;

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

/**
 * @author edusaj
 */
public class DateUtil {

    /**
     * 
     * @param strDate
     * @return  返回日期的汉字格式
     */
    public static final String getBigDateByString(String strDate){
        String[] bigSign={"零","一","二","三","四","五","六","七","八","九"};
        
        String outputDate=null;
        StringBuffer output=new StringBuffer();        
        Date inputDate=getDateByString(strDate);
        output.append(""+"年");
        
        return outputDate;
        
    }
    public static final Date getDateByString(String strDate) {
        Date result = null;

        DateFormat df = DateFormat.getDateInstance();

        try {
            result = df.parse(strDate);
        } catch (java.text.ParseException pe) {

        }

        return result;
    }
    
    public static Date getDateByString(String strDate, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);

        Date result = null;
        try {
            result = format.parse(strDate);
        } catch (java.text.ParseException pe) {

        }
        return result;

    }

    public static String getShortDate(String d) {//得到当前传入日期字符串的短日期格式;

        DateFormat df = DateFormat.getDateInstance();

        String date = "";

        try {
            Date now = df.parse(d);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            date = format.format(now);

        }

        catch (java.text.ParseException pe) {
            return d;
        }

        return date;

    }
   

    public static String getLongDate(String d) { //得到当前传入日期字符串的长日期格式;

        DateFormat df = DateFormat.getDateTimeInstance();

        String date = "";

        if (d == null || d.equals("")) {
            return "";
        }

        try {
            Date now = df.parse(d);

            SimpleDateFormat format = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            date = format.format(now);

        }

        catch (java.text.ParseException pe) {
            return d;
        }

        return date;

    }

    public static String getNowFormatDate(String pattern) {
        Date now = new Date();

        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(now);
    }

    /** 得到当前日期格式为:yyyy-MM-dd HH:mm:ss */
    public static String getNowDateTime() {
        return getNowFormatDate("yyyy-MM-dd HH:mm:ss");
    }   
    
    public static String getNowDate(){
    	return getNowFormatDate("yyyy-MM-dd");
    }
    public static String getNowFormatDate() {
        return getFormatDate("all");
    }

    public static String getFormatDate(String type) {

        Date now = new Date();

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = format.format(now);

        if (type.trim().equalsIgnoreCase("year")) {
            return date.substring(0, 4);
        } else if (type.trim().equalsIgnoreCase("month")) {
            return date.substring(4, 6);
        } else if (type.trim().equalsIgnoreCase("day")) {
            return date.substring(6, 8);
        } else {
            return date;
        }
    }

    /**
     * 得到指定时间以后指定秒数的时间
     * 
     * @param beginDate
     *            指定时间"yyyy-MM-dd HH:mm:ss"
     * @param paramSecond
     *            指定秒数
     * @return
     */
    public static String getDateAfter(String beginDateTime, int paramSecond) {
        Date beginDate;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始时间
        try {
            beginDate = sdf.parse(beginDateTime);
        } catch (Exception e) {
            return null;
        }
        return getDateAfter(beginDate, paramSecond);
    }

    public static String getDateAfter(Date beginDateTime, int paramSecond) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df = DateFormat.getInstance();
        Calendar cd = Calendar.getInstance();
        cd.setTime(beginDateTime);
        cd.add(Calendar.SECOND, paramSecond);
        String endRingDateTime = sdf.format(cd.getTime());
        return endRingDateTime;
    }

    /** 得到当前的微秒 */
    public static long getLongTime() {
        return System.currentTimeMillis();
    }
    
    /**
     * 比较2个日期
     * strDate1 : 日期1
     * strDate   : 2005-12-14 13:13:43
     * type : 计算类型,有 year month day hour minute second
     * param : 值
     * 返回日期类型
     */
    public static int getCompareToParam(String strDate1,String strDate2)
    {
        
        if(strDate1.compareTo(strDate2) > 0 )
            return 1;
        else if(strDate1.compareTo(strDate2) < 0 )
            return -1;
        else
            return 0;        
    }
    
     /**
         * 两个日期之间相隔天数
         * @param from 开始时间
         * @param to 结束时间
         * @return 天数
         */
        public static String getDaysBetweenTwoDates(String dateFrom) {
            Date dtFrom = null;
            Date dtEnd = null;
            String dateEnd = DateUtil.getNowDateTime();
            dtFrom = toDate(dateFrom, "yyyyMMdd");
            dtEnd = toDate(dateEnd, "yyyyMMdd");
            long begin = dtFrom.getTime();
            long end = dtEnd.getTime();
            long inter = end - begin;
            if (inter < 0) {
                inter = inter * (-1);
            }
            long dateMillSec = 24 * 60 * 60 * 1000;     
            long dateCnt =  inter / dateMillSec;         
            long remainder = inter % dateMillSec;           
            if (remainder != 0) {
                dateCnt++;
            }
            return String.valueOf(dateCnt);
        }


        /**
         * 字符窜(yyyyMMdd)转换成为java.util.Date
         * 
         * @param sDate 字符窜(yyyyMMdd)
         * @param sFmt format 
         * @return Date java.util.Date日期
         */
        public static Date toDate(String sDate, String sFmt) {
            Date dt = null;
            try {
                dt = new SimpleDateFormat(sFmt).parse(sDate);
            } catch (java.text.ParseException e) {
                return dt;
            }
            return dt;
        }
      
      
       

   
}

⌨️ 快捷键说明

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