时间转换工具类

日期转获取字符串

    /**
     * 日期转获取字符串
     *
     * @param date日期
     * @param format格式 ("yyyy-MM-dd hh:mm:ss")
     *                 hh:表示12时制
     *                 HH 表示24时制
     * @return
     */
    public static String getDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

系统当前时间

    /**
     * 获取系统当前日期(格式2012-12-28 13:58:00)
     *
     * @return
     */
    public static String getSysDate() {
        return getDate(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss");
    }

获取小时和分钟

    /**
     * 获取小时
     * @return
     */
    public static int getHourOfDay() {
        return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取分钟
     * @return
     */
    public static int getMinute() {
        return Calendar.getInstance().get(Calendar.MINUTE);
    }

    /**
     * 获取小时
     *
     * @return
     */
    public static int getHourOfDay(long time) {
        Date date = new Date(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

获取一天中时分秒

    /**
     * 取出时分秒,并转化为long类型
     * 2017-12-13 15:47:43 -> 15:47:43 -> 56863000
     */
    public static long filterHms(long curTime) {
        String str = getDate(curTime, "HH:mm:ss");
        String time[] = str.split(":");
        int hour = Integer.parseInt(time[0]);
        int minute = Integer.parseInt(time[1]);
        int second = Integer.parseInt(time[2]);
        return hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
    }

DateTimeUtil工具栏

package com.sufadi.nightmode.util;

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

public class DateTimeUtil {

    /**
     * 获取系统当前日期(格式2012-12-28 13:58:00)
     *
     * @return
     */
    public static String getSysDate() {
        return getDate(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getSysDate(long time) {
        return getDate(time, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取系统当前日期(格式2012-12-28)
     *
     * @return
     */
    public static String getSysDateYMD() {
        return getDate(new Date(System.currentTimeMillis()), "yyyy-MM-dd");
    }

    /**
     * 获取系统当前时间(格式13:58:00)
     *
     * @return
     */
    public static String getSysTimeHMS() {
        return getDate(new Date(System.currentTimeMillis()), "HH:mm:ss");
    }

    /**
     * 日期转获取字符串
     *
     * @param date日期
     * @param format格式 ("yyyy-MM-dd hh:mm:ss")
     *                 hh:表示12时制
     *                 HH 表示24时制
     * @return
     */
    public static String getDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String getDate(long time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time));
    }

    /**
     * 获取天数
     * @return
     */
    public static int getDayOfYear() {
        return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
    }

    /**
     * 获取小时
     * @return
     */
    public static int getHourOfDay() {
        return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取小时
     *
     * @return
     */
    public static int getHourOfDay(long time) {
        Date date = new Date(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取分钟
     * @return
     */
    public static int getMinute() {
        return Calendar.getInstance().get(Calendar.MINUTE);
    }

    /**
     * 获取分钟
     *
     * @return
     */
    public static int getMinute(long time) {
        Date date = new Date(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 取出时分秒,并转化为long类型
     * 2017-12-13 15:47:43 -> 15:47:43 -> 56863000
     */
    public static long filterHms(long curTime) {
        String str = getDate(curTime, "HH:mm:ss");
        String time[] = str.split(":");
        int hour = Integer.parseInt(time[0]);
        int minute = Integer.parseInt(time[1]);
        int second = Integer.parseInt(time[2]);
        return hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值