时间工具类

在开发项目的时候,一般都会有时间转换,然后这边整理了一些时间转换的方法,方便快速去设置需要的格式时间

这个是后来项目中添加的工具类,有一些还没有进行添加上去。


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

/**
 * Created by Administrator on 2017/9/4.
 * description: 时间工具类
 */

public class DateUtils {
    public static String DateStyleYMD_HMS = "yyyy-MM-dd HH:mm:ss";
    public static String DateStyleYMD_HZZ = "yyyy-MM-dd HH:00:00";
    public static String DateStyleYMD_HMZ = "yyyy-MM-dd HH:mm:00";
    public static String DateStyleYMD = "yyyy-MM-dd";
    public static String DateStyleYMDHM = "yyyy-MM-dd HH:mm";
    public static String DateStyleMDHM = "MM-dd HH:mm";
    public static String DateStyleHMS = "HH:mm:ss";
    public static String DateStyleHM = "HH:mm";
    public static String DateStyle_Y = "yyyy";
    public static String DateStyle_M = "MM";
    public static String DateStyle_D = "dd";
    public static String DateStyle_H = "HH";
    public static String DateStyle_m = "mm";
    public static String DateStyle_S = "SS";


    /**
     * 根據String和傳入格式返回一个时间戳
     *
     * @return
     */
    public static long getLongDate(String time, String style) {
        SimpleDateFormat sdf = new SimpleDateFormat(style);
        Date dt = new Date();
        long rTime = 0;
        try {
            dt = sdf.parse(time);//先有string转化为date,
            rTime = dt.getTime();//再有date转化为时间戳
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return rTime;
    }

    /**
     * 将时间错转换成指定格式的字符串
     *
     * @param log_startTime
     * @param dateStyleHMS
     * @return
     */
    public static String DateForMumber(long log_startTime, String dateStyleHMS) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateStyleHMS);
        String re_StrTime = sdf.format(new Date(log_startTime));
        return re_StrTime;
    }

    /**
     * 获取当前的时间
     *
     * @param type
     * @return
     */
    public static long getNowTime(String type) {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat(type);
        String time = format.format(date);
        return getLongDate(time, type);
    }

    /***
     * 根据传过来的String,和type , 转换成要输出的Type
     *
     * @param time 時間
     * @param intoType 傳入格式
     * @param inPutType 輸出格式
     * @return
     */
    public static String upDateState(String time, String intoType, String inPutType) {
        return DateForMumber(getLongDate(time, intoType), inPutType);
    }

    /**
     * 年份
     *
     * @return
     */
    public static int year() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.YEAR);
    }

    /**
     * 月份
     *
     * @return
     */
    public static int month() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.MONTH) + 1;
    }

    /**
     * 日(号)
     *
     * @return
     */
    public static int day() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 小时(点)
     *
     * @return
     */
    public static int hour() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 分钟
     *
     * @return
     */
    public static int minute() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.MINUTE);
    }

    /**
     * 秒
     *
     * @return
     */
    public static int second() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.SECOND);
    }

    /**
     * 获取多少天后的时间搓
     *
     * @param day
     * @return
     */
    public static long getHowDays(int day) {
        //获取当前时间
        long nowTime = getNowTime(DateStyleYMD);
        //获取7天后的时间时间搓
        long addTime = 24 * 60 * 60 * day * 1000;

        return nowTime + addTime;
    }

    /**
     * 获取多少天后的时间搓
     *
     * @param startDay 输入时间
     * @param type     输入时间格式
     * @param day      多少天之后
     * @return
     */
    public static long getHowDays(String startDay, String type, int day) {
        long startTime = getLongDate(startDay, type);
        //获取7天后的时间时间搓
        long addTime = 24 * 60 * 60 * day * 1000;

        return startTime + addTime;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值