Java常用类:Date详解,以及Date,String之间的转换

作为一个稀有的Java妹子,所写的所有博客都只是当作自己的笔记,留下证据自己之前是有用心学习的~哈哈哈哈(如果有不对的地方,也请大家指出,不要悄悄咪咪的不告诉我)

Date

Date是jdk中的日期类,不过里面的很多方法都已经过期了,不能被继续使用,所以现在Date类里能使用的方法不多。

构造方法

这里主要是介绍常用的构造方法,被@Deprecated注解的方法就没列出了。

//无参构造方法,创建当前日期对象
public Date() {
        this(System.currentTimeMillis());
    }
//获取某个时间戳的日期
public Date(long date) {
        fastTime = date;
    }

方法

1.getTime获取当前时间的时间戳

Date date = new Date();
System.out.println(date.getTime());
//1575883504629

2.before(Date date)比较当前日期对象是否在传入对象之前

Date date = new Date();
System.out.println(date.getTime());
//1575883707354
Date date2 = new Date(1575882868765L);
System.out.println(date.before(date2));
//false

3.compareTo比较两个日期的大小

Date date = new Date();
System.out.println(date.getTime());
//1575883788328
Date date2 = new Date(1575882868765L);
System.out.println(date.compareTo(date2));
//1

Date与String的相互转换

因为SimpleDateFormat是线程不安全的,所以下面分享一下用jdk8的LocalDateTime来做时间转换的工具类

public class DateUtils {
    public static final String YYYYMMDDhhmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDD = "yyyy-MM-dd";
    public static final String YYYYMM = "yyyy-MM";
    public static final String MMDD = "MM-dd";
    /**
     * 根据当前时间,加上天数后,返回最大时间
     * @param addDay
     * @return
     */
    public static LocalDateTime getDayMax(int addDay) {
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime = localDateTime.plusDays(addDay);
        localDateTime = LocalDateTime.of(localDateTime.getYear(),localDateTime.getMonth(),localDateTime.getDayOfMonth(),23,59,59);
        return localDateTime;
    }
/**
     * 根据当前时间,加上天数后,返回最小时间
     * 
     * @param addDay 天数
     * @return 
     */
    public static LocalDateTime getDayMin(int addDay) {
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime = localDateTime.plusDays(addDay);
        localDateTime = LocalDateTime.of(localDateTime.getYear(),localDateTime.getMonth(),localDateTime.getDayOfMonth(),0,0,0);
        return localDateTime;
    }
    /**
     * 根据传入日期得到本月月末 最大值
     * @return date 月末日期
     */
    public static LocalDateTime getLastDateOfMonth() {
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime = localDateTime.with(TemporalAdjusters.lastDayOfMonth());
        localDateTime = LocalDateTime.of(localDateTime.getYear(),localDateTime.getMonth(),localDateTime.getDayOfMonth(),23,59,59);
        return localDateTime;
    }
    
    /**
     * 获取几个月后的日期
     * @param month
     * @return
     */
    public static LocalDateTime getPlusMonth(int month){
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime = localDateTime.plusMonths(month);
        return localDateTime;
    }

    /**
     * 获取当月的第一天
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getFirstDayOfMonth(LocalDateTime localDateTime){
        localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth());
        return localDateTime;
    }

    /**
     * 日期转字符串
     * @param date
     * @param format
     * @return
     */
    public static String dateToString(LocalDateTime date,String format){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return formatter.format(date);
    }

    /**
     * 按照默认格式将String转化为Date(yyyy-MM-dd HH:mm:ss)
     */
    public static Date stringToDateDefult(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDhhmmss);
        Date date = null;
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException("时间格式转化错误");
        }
        return date;
    }

    /**
     * LocalDateTime转Date
     * @param date
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime date){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = date.atZone(zoneId);
        return Date.from(zdt.toInstant());

    }

    /**
     * 时间戳转换为日期字符串
     * @param timeStamp
     * @param format
     * @return
     */
    public static String timeStampToString(long timeStamp,String format){
        Instant instant = Instant.ofEpochMilli(timeStamp);
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime dateTime = LocalDateTime.ofInstant(instant, zone);
        return dateToString(dateTime,format);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值