java8 时间工具类

天空的星星 2019-01-19

java string static class pattern parse

自己写的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;
    }
}
登录 后评论
下一篇
我是你爱豆
3714人浏览
2019-08-22
相关推荐
java8新特性
846人浏览
2018-07-27 09:20:00
Java8 Lambda表达式.md
615人浏览
2017-02-25 13:16:00
JAVA8新特性介绍
1024人浏览
2018-10-01 21:12:43
java8学习:入门
653人浏览
2018-11-07 13:58:04
Java Date Time 教程
1028人浏览
2016-04-01 16:47:30
Java Date Time 教程
869人浏览
2017-05-22 15:05:00
java8中的时间处理0 - api介绍
718人浏览
2018-02-22 17:50:00
java8时间工具类
415人浏览
2018-01-31 18:29:00
java8学习:默认方法
576人浏览
2018-11-19 12:16:35
java8学习:新的日期使用
1437人浏览
2018-11-25 11:43:10
0
0
0
1129