java中常用的处理时间方法

来源:互联网 发布:如何学python 编辑:程序博客网 时间:2024/05/21 13:59
java中常用的处理时间方法            
 

package com.sinovatech.infoplat.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* 时间工具类 <strong>时间格式化</strong><br/>
*
* @author 樊海潮
* @date 2009-08-19
*/
public class DateUtil {

    /**
     * 日期格式常量 yyyy-MM-dd
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 时间格式常量 yyyy-MM-dd HH:mm:ss
     */
    public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 按照指定的时间格式将传入的时间转换成字符串
     *
     * @param inputDate
     *            时间
     * @param inputFormat
     *            格式化参数
     * @return
     */
    public static String dateToString(Date inputDate, String inputFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(inputFormat);
        return sdf.format(inputDate);
    }

    /**
     * 按照指定的格式将传入的字符串转换成时间
     *
     * @param inputStr
     *            想要转换成时间的字符串
     * @param inputFormat
     *            时间格式化 年:y 月:M 日:d 时:H 分:m 秒:s
     * @return
     */
    public static Date stringToDate(String inputStr, String inputFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(inputFormat);
        Date date = null;
        try {
            date = sdf.parse(inputStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取当前月的最后一天,并格式化为yyyyMMdd000000 用于次月失效的业务
     *
     * @return
     */
    public static String getLastDayOfMonth() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        GregorianCalendar cal = new GregorianCalendar();
        Calendar cl = Calendar.getInstance();
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cl.set(Calendar.DAY_OF_MONTH, days);
        String timeno = df.format(cl.getTime()) + "000000";

        return timeno;
    }

    /**
     * 把当前时间按照指定的格式格式化并返回
     *
     * @param inputFormat
     *            时间格式
     * @return
     */
    public static String getCurrentTime(String inputFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(inputFormat);
        return sdf.format(new Date());
    }

    /**
     * 获得星期的名字
     *
     * @param weekNo
     * @return
     */
    public static String getWeekName(int weekNo) {
        String[] weekNames = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        String weekName = weekNames[(weekNo - 1)];
        return weekName;
    }

    /**
     * 比较时间
     *
     * @param beginDate
     * @param toDate
     * @return
     */
    public static boolean compareDate(Date beginDate, Date toDate) {
        return (beginDate.before(toDate));
    }

    /**
     *
     * @param beforeArg
     *            开始时间
     * @param afterArg
     *            结束时间
     * @param formatRegex
     *            时间格式化类型 yyyyMMddHHmmss/yyyyMMdd/HHmmss/mmss/等
     * @return
     */
    public static String compareTo(String beforeArg, String afterArg,
            String formatRegex) {
        String result = "";

        SimpleDateFormat format = new SimpleDateFormat(formatRegex);
        Date after = null;
        Date before = null;
        try {
            after = format.parse(afterArg);
            before = format.parse(beforeArg);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (after == null || before == null) {
            throw new NullPointerException();
        }

        long tmp = after.getTime() - before.getTime();
        System.out.println("tmp == " + tmp);
        result = String.valueOf((tmp / 1000 / 60 / 60) + "时" + tmp / 1000 / 60
                % 60 + "分" + tmp / 1000 % 60 + "秒");

        return result;
    }

    public static java.util.Date getTimestamp(String arg) {

        Date d = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            if (arg != null && arg.replaceAll("\\s", "").length() > 0) {
                d = sdf.parse(arg);
                System.out.println(d);

            }

        } catch (ParseException e) {

            e.printStackTrace();
        }

        return d;

    }

    public static java.sql.Date getTimes(String arg) {
        java.sql.Date sd = null;
        java.util.Date d = null;
        if (arg != null) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                d = sdf.parse(arg);
                sd = new java.sql.Date(d.getTime());

            } catch (ParseException e) {

                e.printStackTrace();
            }
        } else {
            sd = new java.sql.Date(System.currentTimeMillis());
        }

        return sd;
    }

    /**
     * 获取前一天的日期
     *
     * @param m
     * @return
     */
    public static String getChangeDate(Date date, int m) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");

        String nowdate = sdf1.format(date);
        Timestamp time = Timestamp.valueOf(nowdate);

        long l = time.getTime() + 24 * 60 * 60 * m * 1000;
        time.setTime(l);
        String changeDate = sdf2.format(time);

        return changeDate;
    }

    public static void main(String[] args) {
        boolean bool = false;
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String s = format.format(date);
        String s1 = s.substring(0, 8) + "000000";
        String s2 = s.substring(0, 8) + "080000";
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = format.parse(s1);
            date2 = format.parse(s2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (date.after(date1) && date.before(date2)) {
            bool = true;
        } else {
            bool = false;
        }

        // System.out.println(dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
        // System.out.println(new Date() + ":" + new Date());
        System.out.println(compareDate(new Date(), new Date()));
    }
}

原创粉丝点击