博客
关于我
Java日期格式转换 Date转String、String转Date
阅读量:224 次
发布时间:2019-03-01

本文共 11098 字,大约阅读时间需要 36 分钟。

package com.example.dateutils;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Calendar;import java.util.GregorianCalendar;public class VeDateUtils {    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");    private static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");    private static final SimpleDateFormat MONTH_DAY_FORMAT = new SimpleDateFormat("MM-dd");    private static final SimpleDateFormat YYYYMMDDHHMMSS_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");    private VeDateUtils() {        // 私密构造,防止单例    }    /**     * 将短时间格式字符串转换为日期     * @param strDate 输入字符串,格式为"yyyy-MM-dd"     * @return 日期对象     */    public static Date strToDate(String strDate) {        try {            ParsePosition pos = new ParsePosition(0);            return DATETIME_FORMAT.parse(strDate, pos);        } catch (Throwable e) {            throw new IllegalArgumentException("无法解析日期字符串:" + strDate, e);        }    }    /**     * 将短时间格式日期转换为字符串     * @param dateDate 日期对象     * @return 字符串,格式为"yyyy-MM-dd"     */    public static String dateToStr(Date dateDate) {        return DATE_FORMAT.format(dateDate);    }    /**     * 将长时间格式字符串转换为日期     * @param strDate 输入字符串,格式为"yyyy-MM-dd HH:mm:ss"     * @return 日期对象     */    public static Date strToDateLong(String strDate) {        try {            ParsePosition pos = new ParsePosition(0);            return DATETIME_FORMAT.parse(strDate, pos);        } catch (Throwable e) {            throw new IllegalArgumentException("无法解析日期字符串:" + strDate, e);        }    }    /**     * 将日期对象转换为长时间格式字符串     * @param dateDate 日期对象     * @return 字符串,格式为"yyyy-MM-dd HH:mm:ss"     */    public static String dateToStrLong(Date dateDate) {        return DATETIME_FORMAT.format(dateDate);    }    /**     * 获取当前时间的长时间格式字符串     * @return 字符串,格式为"yyyy-MM-dd HH:mm:ss"     */    public static String getCurrentDateTime() {        return DATETIME_FORMAT.format(new Date());    }    /**     * 获取当前日期的短时间格式字符串     * @return 字符串,格式为"yyyy-MM-dd"     */    public static String getCurrentDate() {        return DATE_FORMAT.format(new Date());    }    /**     * 获取当前时间的时间格式字符串     * @return 字符串,格式为"HH:mm:ss"     */    public static String getCurrentTime() {        return TIME_FORMAT.format(new Date());    }    /**     * 获取当前日期的月份与日期字符串,格式为"MM-dd"     * @return 字符串,格式为"MM-dd"     */    public static String getCurrentMonthDay() {        return MONTH_DAY_FORMAT.format(new Date());    }    /**     * 获取当前日期的完整年月日字符串,格式为"yyyyMMdd"     * @return 字符串,格式为"yyyyMMdd"     */    public static String getCurrentFullYearMD() {        return YYYYMMDDHHMMSS_FORMAT.format(new Date()).substring(0, 8);    }    /**     * 获取当前日期的完整时间戳字符串,格式为"yyyyMMddHHmmss"     * @return 字符串,格式为"yyyyMMddHHmmss"     */    public static String getCurrentTimestamp() {        return YYYYMMDDHHMMSS_FORMAT.format(new Date());    }    /**     * 计算两个日期之间的天数差     * @param date1 日期1     * @param date2 日期2     * @return 天数差     */    public static long getDaysBetween(Date date1, Date date2) {        return (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);    }    /**     * 判断两个日期是否在同一周     * @param date1 日期1     * @param date2 日期2     * @return 是否在同一周     */    public static boolean isSameWeek(Date date1, Date date2) {        Calendar cal1 = Calendar.getInstance();        Calendar cal2 = Calendar.getInstance();        cal1.setTime(date1);        cal2.setTime(date2);        int week1 = cal1.get(Calendar.WEEK_OF_YEAR);        int week2 = cal2.get(Calendar.WEEK_OF_YEAR);        int yearDiff = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);        if (yearDiff == 0) {            return week1 == week2;        } else if (yearDiff == 1 && cal2.get(Calendar.MONTH) == Calendar.DECEMBER) {            return week1 == week2;        } else if (yearDiff == -1 && cal1.get(Calendar.MONTH) == Calendar.DECEMBER) {            return week1 == week2;        }        return false;    }    /**     * 判断给定日期是否为闰年     * @param ddate 日期字符串,格式为"yyyy-MM-dd"     * @return 是否为闰年     */    public static boolean isLeapYear(String ddate) {        Date date = strToDateLong(ddate);        GregorianCalendar gc = new GregorianCalendar();        gc.setTime(date);        int year = gc.get(Calendar.YEAR);        if ((year % 400) == 0) {            return true;        } else if ((year % 4) == 0) {            if ((year % 100) == 0) {                return false;            } else {                return true;            }        } else {            return false;        }    }    /**     * 获取某月的最后一天     * @param dat 日期字符串,格式为"yyyy-MM-dd"     * @return 最后一天的日期字符串,格式为"yyyy-MM-dd"     */    public static String getEndDateOfMonth(String dat) {        String yearStr = dat.substring(0, 4);        String monthStr = dat.substring(5, 7);        int month = Integer.parseInt(monthStr);        boolean isLeapYear = isLeapYear(dat);        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {            return yearStr + "-" + "31";        } else if (month == 4 || month == 6 || month == 9 || month == 11) {            return yearStr + "-" + "30";        } else {            return isLeapYear ? yearStr + "-" + "29" : yearStr + "-" + "28";        }    }    /**     * 获取当前日期的月份天数字符串     * @return 字符串,格式为"MM-dd"     */    public static String getCurrentMonthDay() {        return MONTH_DAY_FORMAT.format(new Date());    }    /**     * 获取特定日期的月份天数字符串     * @param dat 日期字符串,格式为"yyyy-MM-dd"     * @return 字符串,格式为"MM-dd"     */    public static String getMonthDayStr(String dat) {        String[] parts = dat.split("-");        String month = parts[1];        String day = parts[2];        return month + "-" + day;    }    /**     * 获取特定日期的年月日字符串     * @param dat 日期字符串,格式为"yyyy-MM-dd"     * @return 字符串,格式为"yyyy-MM-dd"     */    public static String getFullYearMD(String dat) {        return dat.substring(0, 8);    }    /**     * 获取特定日期的完整时间戳字符串     * @param dat 日期字符串,格式为"yyyy-MM-dd HH:mm:ss"     * @return 字符串,格式为"yyyyMMddHHmmss"     */    public static String getTimestamp(String dat) {        return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(dat));    }    /**     * 获取当前时间的年月日字符串     * @return 字符串,格式为"yyyy-MM-dd"     */    public static String getCurrentFullYearMD() {        return getCurrentTimestamp().substring(0, 8);    }    /**     * 获取当前时间的完整时间戳字符串     * @return 字符串,格式为"yyyyMMddHHmmss"     */    public static String getCurrentTimestamp() {        return YYYYMMDDHHMMSS_FORMAT.format(new Date());    }    /**     * 获取特定日期的时间戳字符串     * @param dat 日期字符串,格式为"yyyy-MM-dd HH:mm:ss"     * @return 字符串,格式为"yyyyMMddHHmmss"     */    public static String getTimestamp(String dat) {        return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(dat));    }    /**     * 计算两个日期之间的天数     * @param date1 日期1     * @param date2 日期2     * @return 天数差     */    public static long getDaysBetween(Date date1, Date date2) {        return (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);    }    /**     * 获取特定日期的日期字符串     * @param dat 日期字符串,格式为"yyyy-MM-dd"     * @return 字符串,格式为"yyyy-MM-dd"     */    public static String getDateStr(String dat) {        return DATE_FORMAT.format(strToDateLong(dat));    }    /**     * 将日期字符串转换为日期对象     * @param dateStr 日期字符串,格式为"yyyy-MM-dd"     * @return 日期对象     */    public static Date toDate(String dateStr) {        try {            ParsePosition pos = new ParsePosition(0);            return DATETIME_FORMAT.parse(dateStr, pos);        } catch (Throwable e) {            throw new IllegalArgumentException("无法解析日期字符串:" + dateStr, e);        }    }    /**     * 将日期对象转换为日期字符串     * @param date 日期对象     * @return 字符串,格式为"yyyy-MM-dd"     */    public static String dateToStr(Date date) {        return DATE_FORMAT.format(date);    }    /**     * 获取特定日期的周序列     * @param sdate 日期字符串,格式为"yyyy-MM-dd"     * @return 字符串,格式为"yyww"     */    public static String getSeqWeek(String sdate) {        Date date = strToDateLong(sdate);        Calendar c = Calendar.getInstance(Locale.CHINA);        c.setTime(date);        String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));        if (week.length() == 1) {            week = "0" + week;        }        String year = Integer.toString(c.get(Calendar.YEAR));        return year + week;    }    /**     * 获取特定日期的星期几字符串     * @param sdate 日期字符串,格式为"yyyy-MM-dd"     * @return 星期几字符串,格式为"EEEE"     */    public static String getWeekDayStr(String sdate) {        Date date = strToDateLong(sdate);        Calendar c = Calendar.getInstance();        c.setTime(date);        return new SimpleDateFormat("EEEE").format(c.getTime());    }    /**     * 获取特定日期的星期几(1-7)     * @param sdate 日期字符串,格式为"yyyy-MM-dd"     * @return 星期几数字     */    public static int getWeekDayNum(String sdate) {        Date date = strToDateLong(sdate);        Calendar c = Calendar.getInstance();        c.setTime(date);        return c.get(Calendar.DAY_OF_WEEK);    }    /**     * 获取特定日期的星期几名称     * @param sdate 日期字符串,格式为"yyyy-MM-dd"     * @return 星期几名称字符串,格式为"EEEE"     */    public static String getWeekDayName(String sdate) {        return getWeekDayStr(sdate);    }    /**     * 获取特定日期的日期编号     * @param sdate 日期字符串,格式为"yyyy-MM-dd"     * @return 日期编号,格式为"yyyyMMdd"     */    public static String get日期编号(String sdate) {        return getCurrentTimestamp().substring(0, 8);    }    /**     * 获取特定日期的时间戳     * @param sdate 日期字符串,格式为"yyyy-MM-dd HH:mm:ss"     * @return 时间戳字符串,格式为"yyyyMMddHHmmss"     */    public static String getTimestamp(String sdate) {        return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(sdate));    }    /**     * 获取特定日期的月份天数字符串     * @param sdate 日期字符串,格式为"yyyy-MM-dd"     * @return 字符串,格式为"MM-dd"     */    public static String getMonthDayStr(String sdate) {        String[] parts = sdate.split("-");        return parts[1] + "-" + parts[2];    }    /**     * 获取特定日期的年月日字符串     * @param sdate 日期字符串,格式为"yyyy-MM-dd"     * @return 字符串,格式为"yyyy-MM-dd"     */    public static String getFullYearMD(String sdate) {        return sdate.substring(0, 8);    }    /**     * 获取特定日期的完整时间戳字符串     * @param sdate 日期字符串,格式为"yyyy-MM-dd HH:mm:ss"     * @return 字符串,格式为"yyyyMMddHHmmss"     */    public static String getTimestamp(String sdate) {        return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(sdate));    }    /**     * 获取特定日期的月份天数字符串     * @param sdate 日期字符串,格式为"yyyy-MM-dd"     * @return 字符串,格式为"MM-dd"     */    public static String getMonthDayStr(String sdate) {        String[] parts = sdate.split("-");        return parts[1] + "-" + parts[2];    }    /**     * 获取特定日期的年月日字符串     * @param sdate 日期字符串,格式为"yyyy-MM-dd"     * @return 字符串,格式为"yyyy-MM-dd"     */    public static String getFullYearMD(String sdate) {        return sdate.substring(0, 8);    }    /**     * 获取特定日期的完整时间戳字符串     * @param sdate 日期字符串,格式为"yyyy-MM-dd HH:mm:ss"     * @return 字符串,格式为"yyyyMMddHHmmss"     */    public static String getTimestamp(String sdate) {        return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(sdate));    }}

转载地址:http://ddkt.baihongyu.com/

你可能感兴趣的文章
OpenWrt固件编译刷机完全总结
查看>>
Open××× for Linux搭建之二
查看>>
Open×××有线网络时使用正常,无线网络时使用报错的解决方案
查看>>
Opera Mobile Classic Emulator
查看>>
Operation not supported on read-only collection 的解决方法 - [Windows Phone开发技巧系列1]
查看>>
OperationResult
查看>>
Operations Manager 2007 R2系列之仪表板(多)视图
查看>>
operator new and delete
查看>>
operator new 与 operator delete
查看>>
operator() error
查看>>
OPPO K3在哪里打开USB调试模式的完美方法
查看>>
oppo后端16连问
查看>>
OPPO软件商店APP侵权投诉流程
查看>>
Optional用法与争议点
查看>>
Optional类:避免NullPointerException
查看>>
Optional讲解
查看>>
ORA-00069: cannot acquire lock
查看>>
ORA-00923: 未找到要求的 FROM 关键字
查看>>
ORA-00932: inconsistent datatypes: expected - got NCLOB【ORA-00932: 数据类型不一致: 应为 -, 但却获得 NCLOB 】【解决办法】
查看>>
ORA-00942 表或视图不存在
查看>>