Java教程

java日期转换工具类-各种转换和获取(全)

本文主要是介绍java日期转换工具类-各种转换和获取(全),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

java日期转换工具类

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class DateUtil {

    // 从特定格式获取date
    public static Date getDateFromFormat(String dateString, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateString);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }

    // 从date转换成为特定格式
    public static String transformDateToString(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String result = null;
        result = simpleDateFormat.format(date);
        return result;
    }

    public static BigDecimal transFormToBigDecimal(String dateString) {
        if (dateString != null) {
            Date date = DateUtil.getDateFromFormat(dateString, "yyyy-MM-dd");
            String numberString = DateUtil.transformDateToString(date, "yyyyMMdd");
            return new BigDecimal(numberString);
        } else {
            return null;
        }
    }

    public static String transFormOtherFormat(String dateString, String formerFomat, String resultFormat) {
        if (dateString != null) {
            Date date = DateUtil.getDateFromFormat(dateString, formerFomat);
            String numberString = DateUtil.transformDateToString(date, resultFormat);
            return numberString;
        } else {
            return null;
        }
    }

    /**
     * 获取当前日期(yyyyMMdd)
     *
     * @return
     */
    public static String getDateByString() {
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int mounth = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DAY_OF_MONTH);
        String date = year + "";
        if (mounth < 10) {
            date += ("0" + mounth);
        } else {
            date += mounth;
        }

        if (day < 10) {
            date += ("0" + day);
        } else {
            date += day;
        }
        return date;
    }

    /**
     * 获取当前时间(HHmmss)
     *
     * @return
     */
    public static String getTimeByString() {
        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
        String time = "";
        if (hour < 10) {
            time += ("0" + hour);
        } else {
            time += hour;
        }
        if (minute < 10) {
            time += ("0" + minute);
        } else {
            time += minute;
        }
        if (second < 10) {
            time += ("0" + second);
        } else {
            time += second;
        }
        return time;
    }

    /**
     * 获取当前日期(yyyyMMdd)
     *
     * @return
     */
    public static int getDateByInt() {
        return Integer.parseInt(getDateByString());
    }

    /**
     * 获取当前时间(HHmmss)
     *
     * @return
     */
    public static int getTimeByInt() {
        return Integer.parseInt(getTimeByString());
    }

    /**
     * 指定日期前几天的日期
     */
    public static String dateBeforeByString(int date, int days) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date dt = null;
        try {
            dt = sdf.parse(date + "");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.DAY_OF_MONTH, days * (-1));
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 当前日期前几天的日期
     */
    public static String dateBeforeByString(int days) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date dt = null;
        try {
            dt = sdf.parse(getDateByString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.DAY_OF_MONTH, days * (-1));
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 指定日期前几天的日期
     */
    public static int dateBeforeByInt(int date, int days) {
        return Integer.parseInt(dateBeforeByString(date, days));
    }

    /**
     * 当前日期前几天的日期
     */
    public static int dateBeforeByInt(int days) {
        return Integer.parseInt(dateBeforeByString(getDateByInt(), days));
    }

    /**
     * 指定日期后几天的日期
     */
    public static String dateAfterByString(int date, int days) {
        return dateBeforeByString(date, days * (-1));
    }

    /**
     * 当前日期后几天的日期
     */
    public static String dateAfterByString(int days) {
        return dateBeforeByString(getDateByInt(), days * (-1));
    }

    /**
     * 指定日期后几天的日期
     */
    public static int dateAfterByInt(int date, int days) {
        return Integer.parseInt(dateAfterByString(date, days));
    }

    /**
     * 当前日期后几天的日期
     */
    public static int dateAfterByInt(int days) {
        return Integer.parseInt(dateAfterByString(getDateByInt(), days));
    }

    public static long diff(Date start, Date end, TimeUnit unit) {
        long diff = end.getTime() - start.getTime();
        switch (unit) {
            case SECONDS:
                diff = diff / 1000;
                break;
            case MINUTES:
                diff = diff / (60 * 1000);
                break;
            case HOURS:
                diff = diff / (60 * 60 * 1000);
                break;
            case DAYS:
                diff = diff / (24 * 60 * 60 * 1000);
                break;
            default:
                break;
        }
        return diff;
    }

    public static Date getMysqlDateTime(Date date) {
        SimpleDateFormat temp = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String dateTmieStr = temp.format(date);
        try {
            return temp.parse(dateTmieStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Integer getLastMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        int year = calendar.get(Calendar.YEAR);
        int mounth = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        String date = year + "";
        if (mounth < 10) {
            date += ("0" + mounth);
        } else {
            date += mounth;
        }

        if (day < 10) {
            date += ("0" + day);
        } else {
            date += day;
        }
        return Integer.parseInt(date);
    }

    public static Integer getLastMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);

        int year = calendar.get(Calendar.YEAR);
        int mounth = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        String date = year + "";
        if (mounth < 10) {
            date += ("0" + mounth);
        } else {
            date += mounth;
        }

        if (day < 10) {
            date += ("0" + day);
        } else {
            date += day;
        }
        return Integer.parseInt(date);
    }
   /**
     * 把当前日期转成毫秒值
     * @return
     */
    public Long currentDateTurnMillisecondValue() {
        //把当前日期转成毫秒值
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long time = System.currentTimeMillis();
        try {
            Date d = new Date();
            String dateStrs = dateformat.format(d);
            time = dateformat.parse(dateStrs).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 毫秒值转为日期格式(yyyy-MM-dd HH:mm:ss)
     * @param time
     * @return
     */
    public String millsecondValueTurnCurrentDate(String time){
        long createTimeSeconds = Long.valueOf(time);
        Date da = new Date();
        da.setTime(createTimeSeconds);
        String create_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(da);
        return create_time;
    }
}
这篇关于java日期转换工具类-各种转换和获取(全)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!