时间转换工具类

package com.stylefeng.guns.core.util;

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

/**
 * 时间转换工具
 * @Author: dingqi
 * @Date: 2018/7/26
 */
public class TimeUtil {

    /**
     * 时间戳转换显示(yyyy-MM-dd HH:mm:ss)
     * @param timestamp
     * @return
     */
    public static String toDate(String timestamp){
        StringBuffer sbf=new StringBuffer();
        sbf.append(timestamp);
        if(timestamp.length() == 10){
            sbf.append("000");
        }
        long time = Long.parseLong(sbf.toString());  //int放不下的,用long
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return sdf.format(time);
    }

    /**
     * 时间戳转换显示(yyyy-MM-dd HH:mm:ss)
     * @param timestamp
     * @return
     */
    public static String toDate2(String timestamp){
        StringBuffer sbf=new StringBuffer();
        sbf.append(timestamp);
        if(timestamp.length() == 10){
            sbf.append("000");
        }
        long time = Long.parseLong(sbf.toString());  //int放不下的,用long
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");

        return sdf.format(time);
    }

    /**
     * 时间戳转换显示(yyyy-MM-dd)
     * @param timestamp
     * @return
     */
    public static String toDate1(String timestamp){
        StringBuffer sbf=new StringBuffer();
        sbf.append(timestamp);
        if(timestamp.length() == 10){
            sbf.append("000");
        }
        long time = Long.parseLong(sbf.toString());  //int放不下的,用long
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(time);
    }

    /**
     * 时间显示转10位时间戳(yyyy-MM-dd HH:mm:ss)
     * @return
     */
    public static String toTimestamp10(String date) throws ParseException {
       return String.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime()).substring(0,10);
    }

    public static String toTimestamp11(String date) throws ParseException {
        return String.valueOf(new SimpleDateFormat("yyyy-MM-dd").parse(date).getTime()).substring(0,10);
    }


    /**
     * 时间转换string(yyyy-MM-dd)
     * @return
     */
    public static String DatetoTime(Date date) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值