时间格式处理工具类

时间格式工具类

public class TimeFormat {
    public static long currentDiff(long currentMillis, long preMillis) {
        long diff = 0L;
        diff = (currentMillis - preMillis) / 1000L;
        return diff;
    }

    public static String millisToDate(long millis) {
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(millis);
        String dateStr = dateformat.format(gc.getTime());
        return dateStr;
    }
    public static String millisToDate(String millis) {
        long longMillis = Long.parseLong(millis);
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(longMillis);
        String dateStr = dateformat.format(gc.getTime());
        return dateStr;
    }

    public static long dateToMillis(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long milles = 0L;
        try {
            milles = dateFormat.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return milles;
    }

    public static String getCurrentDateString() {
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = df.format(day);
        return dateString;
    }
    /**
     * 把 Jul 8, 2020 12:00:00 AM 格式的时间转换为 2020-07-08 12:00:00 格式的时间
     *
     * @param date Jul 8, 2020 12:00:00 AM 格式的时间
     * @param type true 返回年月日格式  false 返回年月日时分秒
     */
    public static Long parse_date(String date, boolean type) {
        if (date.equals("0"))
            return -1L;
        SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);
        Date d2 = null;
        try {
            //把Jul 8, 2020 12:00:00 AM格式转换为常规的Date格式
            d2 = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat simpleDateFormat;
        if (type) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        } else {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        //再把Date的时间转换为需要的格式
        String format = simpleDateFormat.format(d2);
        long dateToMillis = dateToMillis(format);
        return dateToMillis;
    }

    /**
     * 把 2020-07-08 12:00:00 格式的时间转换为 Jul 8, 2020 12:00:00 AM 格式的时间
     *
     * @param date 年月日时分秒 格式的时间
     */
    public static String format_date(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d2 = null;
        try {
            //把 2020-07-08 12:00:00 格式的时间转换为 常规Date时间
            d2 = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);
        //再把 常规Date时间转换回 Jul 8, 2020 12:00:00 AM 格式的时间
        String format = sdf.format(d2);
        return format;
    }
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值