自制日期工具类

持续更新(19.2.14)
萌新无聊写的日期类,说不定哪一天会用到

序号主要功能参数返回结果例子
1计算出当前年份还剩多少天 单位:天-231
2计算出现在已经过了今年的百分之几 范围(0.0d~1.0d)-0.01268574
3显示现在的季节(不规范,按照个人喜好规定)-
显示指定日期的季节(不规范)17-10-4 , yy-M-d
4生成日期时间-2019/2/14 18:12:29
M/d HH:mm:ss2/14 18:12:29
5计算2天之间的距离2016/12/4 , 2017/12/5366
16/12/4 , 17/12/5 , yy/M/d , DateUtils.DAY366

为了便捷这里出一个来自网上的常用日期格式对照表

日期/时间格式中的字母及其含义与示例
字母 含义 示例
y 年份。一般用 yy 表示两位年份,yyyy 表示 4 位年份 使用 yy 表示的年扮,如 11;
使用 yyyy 表示的年份,如 2011
M 月份。一般用 MM 表示月份,如果使用 MMM,则会
根据语言环境显示不同语言的月份
使用 MM 表示的月份,如 05;
使用 MMM 表示月份,在 Locale.CHINA
语言环境下,如“十月”;在 Locale.US
语言环境下,如 Oct
d 月份中的天数。一般用 dd 表示天数 使用 dd 表示的天数,如 10
D 年份中的天数。表示当天是当年的第几天, 用 D 表示 使用 D 表示的年份中的天数,如 295
E 星期几。用 E 表示,会根据语言环境的不同, 显示不
同语言的星期几
使用 E 表示星期几,在 Locale.CHINA 语
言环境下,如“星期四”;在 Locale.US 语
言环境下,如 Thu
H 一天中的小时数(0~23)。一般用 HH 表示小时数 使用 HH 表示的小时数,如 18
h 一天中的小时数(1~12)。一般使用hh表 示小时数 使用 hh 表示的小时数,如 10 (注意 10 有
可能是 10 点,也可能是 22 点)
m 分钟数。一般使用 mm 表示分钟数 使用 mm 表示的分钟数,如 29
s 秒数。一般使用 ss 表示秒数 使用 ss 表示的秒数,如 38
S 毫秒数。一般使用 SSS 表示毫秒数 使用 SSS 表示的毫秒数,如 156
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期时间工具类
 * 1.生成日期的指定格式
 * 2.日起计算
 */
public class DateUtils {
    /*选择返回计算日期的单位*/
    public static final int DAY = 120001;
    public static final int HOUR = 120002;
    public static final int MINUTE = 120003;
    public static final int SECOND = 120004;
    public static final int WEEK = 120005;

    /**
     * 计算出今年还剩多少天
     * @return
     */
    public static int lastForYear() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int today = cal.get(Calendar.DAY_OF_YEAR);
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return 366 - today;
        }else {
            return 365 - today;
        }
    }

    /**
     * 计算出现在已经过了今年的多少%
     * @return double
     */
    public static double proForYear() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int today = cal.get(Calendar.DAY_OF_YEAR);
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return today / 366.0;
        }else {
            return today / 365.0;
        }
    }

    /**
     * 生成现在的季节
     * @return
     */
    public static String genSeason(){
        //            Date d = new SimpleDateFormat("yyyy-M-d").parse(date);
        int month = Calendar.getInstance().get(Calendar.MONTH);
//            int month = d.getMonth() + 1;
        switch (month + 1){
            case 2:
            case 3:
            case 4:return "春";
            case 5:
            case 6:
            case 7:return "夏";
            case 8:
            case 9:
            case 10:return "秋";
            case 11:
            case 12:
            case 1:return "冬";
            default:return "未知";
        }
    }

    /**
     * 生成指定日期的季节
     * @param date 指定日期
     * @param format 日期格式
     * @return 季节字符串
     */
    public static String genSeason(String date,String format){
        try {
            Date d = new SimpleDateFormat(format == null ? "yyyy-M-d" : format).parse(date);
            int month = d.getMonth() + 1;
            switch (month){
                case 2:
                case 3:
                case 4:return "春";
                case 5:
                case 6:
                case 7:return "夏";
                case 8:
                case 9:
                case 10:return "秋";
                case 11:
                case 12:
                case 1:return "冬";
                default:return "未知";
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "未知";
        }
    }

    /**
     * 生成日期时间
     * @return 字符串 - yyyy/MM/dd
     */
    public static String genMomentDate() {
        return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
    }

    /**
     * 生成日期时间 - 高级
     * @param format 返回指定格式
     * @return -> format
     */
    public static String genMomentDate(String format) {
        format = format == null ? "yyyy/MM/dd HH:mm:ss" : format;
        return new SimpleDateFormat(format).format(new Date());
    }

    /**
     * 计算2天的距离
     *
     * @param beginDate 开始日期
     * @param endDate   结束日期
     * @return 天
     */
    public static int resultDays(String beginDate, String endDate) {
        int finalDate = 0;
        try {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-M-d");
            Date d1 = sd.parse(beginDate);
            Date d2 = sd.parse(endDate);
            final int resultDate = (int) ((d2.getTime() - d1.getTime()) / (60 * 60 * 1000 * 24));
            finalDate = resultDate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return finalDate;
    }

    /**
     * 计算2天的距离 - 高级使用
     * @param beginDate  开始日期
     * @param endDate    结束日期
     * @param format     传入日期格式,如空为默认
     * @param returnType 返回单位
     * @return -> returnType
     */
    public static int resultDays(String beginDate, String endDate, String format, int returnType) {
        try {
            SimpleDateFormat sd = new SimpleDateFormat(format == null ? "yyyy-M-d" : format);
            Date d1 = sd.parse(beginDate);
            Date d2 = sd.parse(endDate);
            long resultDate = (d2.getTime() - d1.getTime());
            switch (returnType) {
                case WEEK:
                    return (int) (resultDate / (1000 * 60 * 60 * 24 * 7));
                case DAY:
                    return (int) (resultDate / (1000 * 60 * 60 * 24));
                case HOUR:
                    return (int) (resultDate / (1000 * 60 * 60));
                case MINUTE:
                    return (int) (resultDate / (1000 * 60));
                case SECOND:
                    return (int) (resultDate / (1000));
                //返回类型错误
                default:
                    return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值