博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DateTimeFormatter时间工具类
阅读量:4182 次
发布时间:2019-05-26

本文共 5996 字,大约阅读时间需要 19 分钟。

package com.xiaobu.base.utils;import java.time.*;import java.time.format.DateTimeFormatter;import java.time.temporal.ChronoUnit;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/1/11 11:52 * @description V1.0 LocalDateTime工具类 * SQL -> Java * date -> LocalDate * time -> LocalTime * timestamp -> LocalDateTime */public class DateTimeUtils {    public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmmss");    public static final DateTimeFormatter YEAR_MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");    public static final DateTimeFormatter SHORT_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyMMdd");    public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");    public static final DateTimeFormatter SHORT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss");    public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");    public static final DateTimeFormatter LONG_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");    /**     * 返回当前的日期     */    public static LocalDate getCurrentLocalDate() {        return LocalDate.now();    }    /**     * 返回当前时间     */    public static LocalTime getCurrentLocalTime() {        return LocalTime.now();    }    /**     * 返回当前日期时间     */    public static LocalDateTime getCurrentLocalDateTime() {        return LocalDateTime.now();    }    /**     * yyyy-MM-dd     */    public static String getCurrentDateStr() {        return LocalDate.now().format(DATE_FORMATTER);    }    /**     * yyMMdd     */    public static String getCurrentShortDateStr() {        return LocalDate.now().format(SHORT_DATE_FORMATTER);    }    public static String getCurrentMonthStr() {        return LocalDate.now().format(YEAR_MONTH_FORMATTER);    }    /**     * yyyy-MM-dd HH:mm:ss     */    public static String getCurrentDateTimeStr() {        return LocalDateTime.now().format(DATETIME_FORMATTER);    }    public static String getCurrentLongDateTimeStr(){        return LocalDateTime.now().format(LONG_DATETIME_FORMATTER);    }    /**     * yyMMddHHmmss     */    public static String getCurrentShortDateTimeStr() {        return LocalDateTime.now().format(SHORT_DATETIME_FORMATTER);    }    /**     * HHmmss     */    public static String getCurrentTimeStr() {        return LocalTime.now().format(TIME_FORMATTER);    }    public static String getCurrentDateStr(String pattern) {        return LocalDate.now().format(DateTimeFormatter.ofPattern(pattern));    }    public static String getCurrentDateTimeStr(String pattern) {        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern));    }    public static String getCurrentTimeStr(String pattern) {        return LocalTime.now().format(DateTimeFormatter.ofPattern(pattern));    }    public static LocalDate parseLocalDate(String dateStr, String pattern) {        return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern));    }    public static LocalDateTime parseLocalDateTime(String dateTimeStr, String pattern) {        return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(pattern));    }    public static LocalTime parseLocalTime(String timeStr, String pattern) {        return LocalTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern));    }    public static String formatLocalDate(LocalDate date, String pattern) {        return date.format(DateTimeFormatter.ofPattern(pattern));    }    public static String formatLocalDateTime(LocalDateTime datetime, String pattern) {        return datetime.format(DateTimeFormatter.ofPattern(pattern));    }    public static String formatLocalTime(LocalTime time, String pattern) {        return time.format(DateTimeFormatter.ofPattern(pattern));    }    public static LocalDate parseLocalDate(String dateStr) {        return LocalDate.parse(dateStr, DATE_FORMATTER);    }    public static LocalDateTime parseLocalDateTime(String dateTimeStr) {        return LocalDateTime.parse(dateTimeStr, DATETIME_FORMATTER);    }    public static LocalDateTime parseLongLocalDateTime(String longDateTimeStr){        return LocalDateTime.parse(longDateTimeStr, LONG_DATETIME_FORMATTER);    }    public static LocalTime parseLocalTime(String timeStr) {        return LocalTime.parse(timeStr, TIME_FORMATTER);    }    public static String formatLocalDate(LocalDate date) {        return date.format(DATE_FORMATTER);    }    public static String formatLocalDateTime(LocalDateTime datetime) {        return datetime.format(DATETIME_FORMATTER);    }    public static String formatLocalTime(LocalTime time) {        return time.format(TIME_FORMATTER);    }    /**     * 日期相隔秒     */    public static long periodHours(LocalDateTime startDateTime,LocalDateTime endDateTime){       return Duration.between(startDateTime, endDateTime).get(ChronoUnit.SECONDS);    }    /**     * 日期相隔天数     */    public static long periodDays(LocalDate startDate, LocalDate endDate) {        return startDate.until(endDate, ChronoUnit.DAYS);    }    /**     * 日期相隔周数     */    public static long periodWeeks(LocalDate startDate, LocalDate endDate) {        return startDate.until(endDate, ChronoUnit.WEEKS);    }    /**     * 日期相隔月数     */    public static long periodMonths(LocalDate startDate, LocalDate endDate) {        return startDate.until(endDate, ChronoUnit.MONTHS);    }    /**     * 日期相隔年数     */    public static long periodYears(LocalDate startDate, LocalDate endDate) {        return startDate.until(endDate, ChronoUnit.YEARS);    }    /**     * 是否当天     */    public static boolean isToday(LocalDate date) {        return getCurrentLocalDate().equals(date);    }    /**     * 获取当前毫秒数     */    public static Long toEpochMilli(LocalDateTime dateTime) {        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();    }    /**     * 判断是否为闰年     */    public static boolean isLeapYear(LocalDate localDate){        return localDate.isLeapYear();    }}

 

转载地址:http://jfgai.baihongyu.com/

你可能感兴趣的文章
Observer模式
查看>>
高性能服务器设计
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Pentaho BI开源报表系统
查看>>
Pentaho 开发: 在eclipse中构建Pentaho BI Server工程
查看>>
android中SharedPreferences的简单例子
查看>>
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Hibernate和IBatis对比
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Android 的source (需安装 git repo)
查看>>
Ubuntu Navicat for MySQL安装以及破解方案
查看>>
java多线程中的join方法详解
查看>>
在C++中如何实现模板函数的外部调用
查看>>
HTML5学习之——HTML 5 应用程序缓存
查看>>
HTML5学习之——HTML 5 服务器发送事件
查看>>
SVG学习之——HTML 页面中的 SVG
查看>>
SVG 滤镜学习之——SVG 滤镜
查看>>
mysql中用命令行复制表结构的方法
查看>>
hbase shell出现ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException
查看>>