java8 时间工具类

自己写的java8时间类的工具类

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

public class DateUtils {

    private static final String DEAFAULT_DATA_PATTERN="yyyy-MM-dd";
    private static final String DEAFAULT_TIME_PATTERN="HH:mm:ss";
    private static final String DEAFAULT_DATATIME_PATTERN="yyyy-MM-dd HH:mm:ss";

    /** String ---> LocalDate */
    public static LocalDate parseToLocalDate(String dateStr){
        LocalDate localDate=parseToLocalDate(dateStr, DEAFAULT_DATA_PATTERN);
        return localDate;
    }

    public static LocalDate parseToLocalDate(String dateStr,String pattern){
        LocalDate localDate=LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
        return localDate;
    }

    /** String ---> LocalDateTime */
    public static LocalDateTime parseToLocalDateTime(String dateTimeStr){
        LocalDateTime localDatetime=parseToLocalDateTime(dateTimeStr,DEAFAULT_DATATIME_PATTERN);
        return localDatetime;
    }

    public static LocalDateTime parseToLocalDateTime(String dateTimeStr,String pattern){
        LocalDateTime localDatetime=LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(pattern));
        return localDatetime;
    }

    /** String ---> LocalTime */
    public static LocalTime parseToLocalTime(String timeStr){
        LocalTime localTime=parseToLocalTime(timeStr, DEAFAULT_TIME_PATTERN);
        return localTime;
    }

    public static LocalTime parseToLocalTime(String timeStr,String pattern){
        LocalTime localTime=LocalTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern));
        return localTime;
    }

    /** LocalTime / LocalDate / LocalDateTime ---> String */
    public static String formatToString(Temporal temporal,String pattern){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
        String temporalStr=df.format(temporal);
        return temporalStr;
    }

    /**  LocalDate / LocalDateTime ---> String("yyyy-MM-dd") */
    public static String formatLoalDate(Temporal temporal){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(DEAFAULT_DATA_PATTERN);
        String temporalStr=df.format(temporal);
        return temporalStr;
    }

    /** LocalTime / LocalDateTime ---> String ("HH:mm:ss") */
    public static String formatLoalTime(Temporal temporal){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(DEAFAULT_TIME_PATTERN);
        String temporalStr=df.format(temporal);
        return temporalStr;
    }

    /** LocalDate / LocalDateTime ---> String ("yyyy-MM-dd HH:mm:ss")*/
    public static String formatLoalDateTime(Temporal temporal){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(DEAFAULT_DATATIME_PATTERN);
        String temporalStr=df.format(temporal);
        return temporalStr;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值