java8 时间类的基本使用

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * description: java8 时间工具类
 * 
 * @date 2019年9月23日
 */
public class DateUtils {

    private DateUtils() {

    }

    /**
     * description: 获取当前日期
     * 
     * @return 2019-09-23
     */
    public static String getNowDate() {
        LocalDate localDate = LocalDate.now();
        return localDate.toString();
    }

    /**
     * description: 获取 pattern 格式的当前日期
     * 
     * @param pattern
     *            日期格式
     * @return
     */
    public static String getNowDateWithPattern(String pattern) {
        LocalDate localDate = LocalDate.now();
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * description: 将字符串转化为 LocalDate 格式
     * 
     * @param date
     *            日期字符串
     * @param pattern
     *            日期格式
     * @return
     */
    public static LocalDate formatToDate(String date, String pattern) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * description: 获取昨天的日期
     * 
     * @return
     */
    public static String getLastDay() {
        LocalDate localDate = LocalDate.now();
        LocalDate lastDay = localDate.minusDays(1);
        return lastDay.toString();
    }

    /**
     * description: 获取当前时间
     * 
     * @return 22:00:39.310
     */
    public static String getNowTime() {
        return LocalTime.now().toString();
    }

    public static String getNowTime(String pattern) {
        return LocalTime.now().format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * description: 获取当前日期时间
     * 
     * @return
     */
    public static String getNowDateTime() {
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.toString();
    }

    /**
     * description: 获取当前日期时间
     * 
     * @param pattern
     * @return
     */
    public static String getNowDateTime(String pattern) {
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern == null ? "yyyy-MM-dd HH:mm:ss" : pattern));
    }

    public static void main(String[] args) {
        System.out.println(getNowDate());
        System.out.println(getNowDateWithPattern("yyyyMMdd"));
        System.out.println(formatToDate("20190923", "yyyyMMdd"));
        System.out.println(getLastDay());
        System.out.println(getNowTime());
        System.out.println(getNowTime("HH:mm:ss"));
        System.out.println(getNowDateTime());
        System.out.println(getNowDateTime(null));

    }
}
----------------------------------------------------------------
2019-09-23
20190923
2019-09-23
2019-09-22
22:09:41.240
22:09:41
2019-09-23T22:09:41.240
2019-09-23 22:09:41
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值