JAVA--DATE

日期模式:

yyyy:年
MM:月
dd:日
hh:1~12小时制(1-12)
HH:24小时制(0-23)
mm:分
ss:秒
S:毫秒
E:星期几
D:一年中的第几天
F:一月中的第几个星期(会把这个月总共过的天数除以7)
w:一年中的第几个星期
W:一月中的第几星期(会根据实际情况来算)
a:上下午标识
k:和HH差不多,表示一天24小时制(1-24)。
K:和hh差不多,表示一天12小时制(0-11)。
z:表示时区  

 

(一)Calendar 

 public static void main(String[] args) {
            // 获取当前的日历时间
            Calendar c = Calendar.getInstance();
            // 获取年
            int year = c.get(Calendar.YEAR);
            // 获取月
            int month = c.get(Calendar.MONTH);
            // 获取日
            int date = c.get(Calendar.DATE);
            System.out.println(year + "年" + (month + 1) + "月" + date + "日");

             // 三年前的今天
             c.add(Calendar.YEAR, -3);
             // 获取月
             year = c.get(Calendar.YEAR);
             // 获取月
             month = c.get(Calendar.MONTH);
             // 获取日
             date = c.get(Calendar.DATE);
             System.out.println(year + "年" + (month + 1) + "月" + date + "日");

            // 5年后的10天前
            c.add(Calendar.YEAR, 5);
            c.add(Calendar.DATE, -10);
            // 获取月
            year = c.get(Calendar.YEAR);
            // 获取月
            month = c.get(Calendar.MONTH);
            // 获取日
            date = c.get(Calendar.DATE);
            System.out.println(year + "年" + (month + 1) + "月" + date + "日");// 2021年11月30日
            System.out.println("-----------");

            c.set(2011, 11, 11);
            // 获取月
            year = c.get(Calendar.YEAR);
            // 获取月
            month = c.get(Calendar.MONTH);
            // 获取日
            date = c.get(Calendar.DATE);
            System.out.println(year + "年" + (month + 1) + "月" + date + "日");
        }

(二)DateUtils 

package com.gildata.task.platform.common.util.date;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author fanwq
 * @DateTime 2020/11/9 20:13
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2020/11/09
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20201109
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
    /**
     * 计算两个时间差--》得到毫秒级差值
     */
    public static Integer getDateDiff(Date endDate, Date nowDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String endTimeStr=sdf.format(endDate);
        String startTimeStr=sdf.format(nowDate);
        try {
            endDate=sdf.parse(endTimeStr);
            nowDate=sdf.parse(startTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long endTime=  endDate.getTime();
        long startTime= nowDate.getTime();
        int minDiff=(int) (endTime-startTime);
        Integer result = new Integer(minDiff);
        return result;

    }
    /***
     * 获取时期与分钟的加和时期
     * @param date
     * @param minutes
     * @return
     */
    public static Date getDateSum(Date date, int minutes) {
        Date sumDate = new Date(date.getTime() + minutes * 60 * 1000);
        return sumDate;
    }

    /***
     * 日期比较【comp1<comp2 :return true】
     * @param comp1
     * @param comp2
     * @return
     */
    public static Boolean dateCompare(Date comp1, Date comp2) {
        return comp1.before(comp2);
    }
}

https://www.cnblogs.com/yadongliang/p/9649912.html

猜你喜欢

转载自blog.csdn.net/CUITAO2305532402/article/details/111301664