时间转换工具类

来源:互联网 发布:在线相片制作软件 编辑:程序博客网 时间:2024/04/28 09:06

日期转获取字符串

    /**     * 日期转获取字符串     *     * @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;    }}