DateUtils 获取时间工具类

/**
 * Created by DateUtils on 2019/10/14.
 */
public class DateUtils {
    private static final String TAG = "DataUtils";

    public static String getTodayDateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.getDefault());
        return format.format(new Date());
    }

    public static String getTodayDateTime(String dateType) {
        SimpleDateFormat format = new SimpleDateFormat(dateType,
                Locale.getDefault());
        return format.format(new Date());
    }

    /**
     * 时间加减
     *
     * @return
     */
    public static long date2TimeStamp(String start, String end) throws ParseException {
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        Date d1 = df.parse(start);
        Date d2 = df.parse(end);
        long diff = d1.getTime() - d2.getTime();
        return diff;
    }

    /**
     * 掉此方法输入所要转换的时间输入例如("2014年06月14日16时09分00秒")返回时间戳
     *
     * @param time
     * @return
     */
    public static String data(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒",
                Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            String stf = String.valueOf(l);
            times = stf.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }


    /**
     * 调此方法输入所要转换的时间输入例如("2014-06-14-16-09-00")返回时间戳
     *
     * @param time
     * @return
     */
    public static long dataTwo(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            return l;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static String time(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;
    }


    /**
     * 根据传递的类型格式化时间
     *
     * @param str
     * @param type 例如:yy-MM-dd
     * @return
     */
    public static String getDateTimeByMillisecond(String str, String type) {

        Date date = new Date(Long.valueOf(str));

        SimpleDateFormat format = new SimpleDateFormat(type);

        String time = format.format(date);

        return time;
    }

    /**
     * 返回时间戳
     *
     * @return
     */
    public static String getTime() {
        long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳
        String str = String.valueOf(time);
        return str;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值