时间转换类

package *.common.util;

import com.alibaba.druid.util.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 时间转化工具 date转为时间戳 时间戳转date 互相与String的转换
 * 所有出现的String time 格式都必须为(yyyy-MM-dd HH:mm:ss),否则出错
 *
 * @author 
 */
public class TimeExchange {


    /**
     * String(yyyy-MM-dd HH:mm:ss) 转 Date
     * parse: 按指定的源格式把String转换为Date对象
     *
     * @param time
     * @return
     * @throws ParseException
     */
    // String date = "2010/05/04 12:34:23";
    public static Date StringToDate(String time) throws ParseException {

        if (StringUtils.equals(time, "") || time == null) {
            return null;
        }
        Date date = new Date();
        // 注意format的格式要与日期String的格式相匹配
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(time);
            //System.out.println(date.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Date转为String(yyyy-MM-dd HH:mm:ss)
     * format: 按指定的目标格式把Date对象转换为String
     *
     * @param time
     * @return
     */
    public static String DateToString(Date time) {
        String dateStr = "";
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            dateStr = dateFormat.format(time);
            /*System.out.println(dateStr);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }


    /**
     * 根据给定的Date和格式  转换成  String
     */
    public static String DateToStringbyformat(Date time, String format) {
        String dateStr = "";
        DateFormat dateFormat = new SimpleDateFormat(format);
        try {
            dateStr = dateFormat.format(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * 根据给定的String time和格式  转换成  Date
     *
     * @param time
     * @param format
     * @return
     */
    public static Date StringToDatebyformat(String time, String format) {

        if (StringUtils.equals(time, "") || time == null)
            return null;
        if (time.length() != format.length())
            return null;

        Date date = null;
        // 注意format的格式要与日期String的格式相匹配
        DateFormat dateFormat = new SimpleDateFormat(format);
        try {
            date = dateFormat.parse(time);
            date = TimestampToDate(Integer.valueOf(date.getTime() / 1000 + ""));
//          System.out.println(date.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     * Date 转换成 String 格式(yyyy-MM-dd HH:mm)
     * @param time
     * @return
     */
    public static String DateToStringForhhmm(Date time) {
        String dateStr = "";
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            dateStr = dateFormat.format(time);
            /* System.out.println(dateStr);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * Date 转换成String 格式(yyyyMMddHHmmss)
     * @param time
     * @return
     */
    public static String DateToStringSimple(Date time) {
        String dateStr = "";
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            dateStr = dateFormat.format(time);
            /* System.out.println(dateStr);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * String(yyyy-MM-dd HH:mm:ss)  转换为  10位时间戳
     *
     * @param time
     * @return
     */
    public static Integer StringToTimestamp(String time) {

        int times = 0;
        try {
            times = (int) ((Timestamp.valueOf(time).getTime()) / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (times == 0) {
            //System.out.println("String转10位时间戳失败");
        }
        return times;

    }

    /**
     * 10位int型的时间戳  转换为   String(yyyy-MM-dd HH:mm:ss)
     *
     * @param time
     * @return
     */
    public static String TimestampToString(Integer time) {
        //int转long时,先进行转型再进行计算,否则会是计算结束后在转型
        long temp = (long) time * 1000;
        Timestamp ts = new Timestamp(temp);
        String tsStr = "";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //方法一  
            tsStr = dateFormat.format(ts);
            //System.out.println(tsStr);  
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tsStr;
    }

    /**
     * 10位时间戳  转换成 Date
     *
     * @param time
     * @return
     */
    public static Date TimestampToDate(Integer time) {
        long temp = (long) time * 1000;
        Timestamp ts = new Timestamp(temp);
        Date date = new Date();
        try {
            date = ts;
            //System.out.println(date);  
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * long型  转换成  Date类型
     * @param time
     * @return
     */
    public static Date TimestampToDatelong(long time) {
        Timestamp ts = new Timestamp(time);
        Date date = new Date();
        try {
            date = ts;
            //System.out.println(date);  
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Date类型  转换为   10位时间戳
     *
     * @param time
     * @return
     */
    public static Integer DateToTimestamp(Date time) {
        Timestamp ts = new Timestamp(time.getTime());

        return (int) ((ts.getTime()) / 1000);
    }


    /**
     * 获取当前时间的秒数
     */
    public static int getCurrentTimestamp() {
        return (int) (System.currentTimeMillis() / 1000);
    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值