前言:
当前朋友们对“java时间戳转换日期格式”都比较注意,我们都需要分析一些“java时间戳转换日期格式”的相关资讯。那么小编在网上网罗了一些关于“java时间戳转换日期格式””的相关文章,希望各位老铁们能喜欢,咱们一起来学习一下吧!本章主要是基于java8中的一些时间类,写的一些常用的时间转换、时间计算的方法。比如LocalDateTime,LocalDate,Date等。
在开发的很多时候,经常会遇到一些时间的计算和转换,像多少天后是星期几,两个日期相差天数,时间戳转为Date日期类型等等,每次遇到都需要翻看以往的项目代码查找。故在此做一下简单的记录。
1.LocalDateTime类
public class LocalDateTimeTool {
public static void main(String[] args) {
System.out.println(getCurrentDateTime());
System.out.println(getLocalDateTimeByString("2022-05-05 15:35:20.222", FormatPattern.DATE_TIME_FORMAT.getFormat()));
}
/**
* 获取当前日期: yyyy-mm-dd hh:mm:ss.SSS
* @return
*/
public static LocalDateTime getCurrentDateTime() {
return LocalDateTime.now();
}
/**
* 字符串转LocalDate
* @return
*/
public static LocalDateTime getLocalDateTimeByString(String dateString, String format){
if (format == null) {
format = FormatPattern.DATE_TIME_FORMAT.getFormat();
}
return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(format));
}
/**
* LocalDateTime转Date: 默认格式
* @param date
* @return
*/
public static Date getDateByLocalDateTime(LocalDateTime date){
return Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* Date 转 LocalDateTime
* @param date
* @return
*/
public static LocalDateTime getLocalDateTimeByDate(Date date){
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
/**
* 日期时间(LocalDateTime)按指定格式转字符串
*
* @param dateTime
* @param format
* @return
*/
public static String getStringBylocalDateTime(LocalDateTime dateTime, String format) {
String dateString;
if (dateTime == null) {
dateString = "";
} else {
DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(format);
dateString = dateTime.format(formatDate);
}
return dateString;
}
/**
* Date 相差xxx天的日期
* @param date
* @param value
* @param unit
* @return LocalDateTime
*/
public static LocalDateTime getDifferValueOfDateForLocalDateTime(Date date, Integer value, ChronoUnit unit){
LocalDateTime localDateTime = getLocalDateTimeByDate(date);
return localDateTime.plus(value, unit);
}
/**
* LocalDateTime 转 LocalDate
* @param localDateTime
* @return
*/
public static LocalDate getLocalDateByLocalDateTime (LocalDateTime localDateTime) {
return localDateTime.toLocalDate();
}
}
public enum FormatPattern {
DATE_TIME_FORMAT("yyyy-MM-dd HH:mm:ss"),
DATETIME_FORMAT("yyyyMMddHHmmss"),
DATE_FORMAT("yyyy-MM-dd"),
DATEFORMAT("yyyyMMdd"),
;
private String format;
private FormatPattern(String format) {
this.format = format;
}
public String getFormat() {
return format;
}
}
2.LocalDateTime类
public class LocalDateTool {
public static void main(String[] args) {
System.out.println(getCurrentDate());
System.out.println(getLocalDateByString("2015-01-01"));
System.out.println(getDateByLocalDate(LocalDate.now()));
System.out.println(getStartOrEndOfWeek(null, true));
System.out.println(getStartOrEndOfMonth(null, false));
System.out.println(getStartOrEndOfQuarter(null, false));
System.out.println(getStartOrEndOfYear(null, false));
System.out.println(getInstantFromLocalDate(getCurrentDate()));
System.out.println(getDifferValueOfDateForLocalDate(new Date(), -5, ChronoUnitDateEnum.MONTHS));
System.out.println(getDifferValueOfDateForLocalDate(new Date(), -5, ChronoUnitDateEnum.YEARS));
System.out.println(getDifferValueOfDateForLocalDate(new Date(), 14, ChronoUnitDateEnum.DAYS));
}
/**
* 获取当前日期: yyyy-mm-dd
* @return
*/
public static LocalDate getCurrentDate() {
return LocalDate.now();
}
/**
* 字符串转LocalDate
* @return
*/
public static LocalDate getLocalDateByString(String dateString){
return LocalDate.parse(dateString);
}
/**
* LocalDate转Date: 默认格式
* @param date
* @return
*/
public static Date getDateByLocalDate(LocalDate date){
return Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
/**
* Date 转 LocalDate
* @param date
* @return
*/
public static LocalDate getLocalDateByDate(Date date){
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* Date 相差xxx天的日期
* @param date
* @param value
* @param unit
* @return LocalDate
*/
public static LocalDate getDifferValueOfDateForLocalDate(Date date, Integer value, ChronoUnitDateEnum unit){
LocalDate localDate = getLocalDateByDate(date);
switch (unit.getUnit()) {
case DAYS:
return localDate.plusDays(value);
case WEEKS:
return localDate.plusWeeks(value);
case MONTHS:
return localDate.plusMonths(value);
case YEARS:
return localDate.plusYears(value);
default:
return localDate;
}
}
/**
* 获取当前周 开始时间和结束时间
* @param date 日期
* @param isFirstDayOfWeek 是否开始
* @return
*/
public static LocalDate getStartOrEndOfWeek(LocalDate date , Boolean isFirstDayOfWeek){
LocalDate now = LocalDate.now();
if (date == null){
date = now;
}
DayOfWeek dayOfWeek = date.getDayOfWeek();
int value = dayOfWeek.getValue();
if (isFirstDayOfWeek){
now = date.minusDays(value - 1);
}else {
now = date.plusDays(7 - value);
}
return now;
}
/**
* 获取当前周 开始时间和结束时间
* @param date 日期
* @param isFirstDayOfMonth 是否开始
* @return
*/
public static LocalDate getStartOrEndOfMonth(LocalDate date , Boolean isFirstDayOfMonth){
LocalDate now = LocalDate.now();
if (date == null){
date = now;
}
if (isFirstDayOfMonth){
now = date.withDayOfMonth(1);
}else {
now = date.with(TemporalAdjusters.lastDayOfMonth());
}
return now;
}
/**
* 获取当前周 开始时间和结束时间
* @param date 日期
* @param isFirstDayOfQuarter 是否开始
* @return
*/
public static LocalDate getStartOrEndOfQuarter(LocalDate date , Boolean isFirstDayOfQuarter) {
LocalDate now = LocalDate.now();
if (date == null){
date = now;
}
Month month = date.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
if (isFirstDayOfQuarter){
now = LocalDate.of(date.getYear(), firstMonthOfQuarter, 1);
}else {
now = LocalDate.of(date.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(date.isLeapYear()));
}
return now;
}
/**
* 获取当前周 开始时间和结束时间
* @param date 日期
* @param isFirstDayOfYear 是否开始
* @return
*/
public static LocalDate getStartOrEndOfYear(LocalDate date , Boolean isFirstDayOfYear){
LocalDate now = LocalDate.now();
if (date == null){
date = now;
}
if (isFirstDayOfYear){
now = LocalDate.of(date.getYear(), Month.JANUARY, 1);
}else {
now = LocalDate.of(date.getYear(), Month.DECEMBER, Month.DECEMBER.length(date.isLeapYear()));
}
return now;
}
/**
* LocalDate 转换成 Instant时间戳
* @param date
* @return
*/
public static Instant getInstantFromLocalDate(LocalDate date){
return date.atStartOfDay().toInstant(ZoneOffset.UTC);
}
}
public enum ChronoUnitDateEnum {
DAYS(ChronoUnit.DAYS),
WEEKS(ChronoUnit.WEEKS),
MONTHS(ChronoUnit.MONTHS),
YEARS(ChronoUnit.YEARS),
;
private ChronoUnit unit;
private ChronoUnitDateEnum(ChronoUnit unitType) {
this.unit = unitType;
}
public ChronoUnit getUnit() {
return unit;
}
}
3.LocalTime类
public class LocalTimeTool {
public static void main(String[] args) {
System.out.println(getCurrentTime());
System.out.println(getLocalTimeByString("20:15:15.256"));
System.out.println(getDifferValueOfTimeForLocalTime(getCurrentTime(), 50, ChronoUnitTimeEnum.MINUTES));
}
/**
* 获取当前时间: hh:mm:ss.SSS
* @return
*/
public static LocalTime getCurrentTime() {
return LocalTime.now();
}
/**
* 字符串转LocalTime
* @return
*/
public static LocalTime getLocalTimeByString(String dateString){
return LocalTime.parse(dateString);
}
/**
* LocalTime 相差XXX的时间
* @param time
* @param value
* @param unit
* @return
*/
public static LocalTime getDifferValueOfTimeForLocalTime(LocalTime time, Integer value , ChronoUnitTimeEnum unit){
switch (unit.getUnit()) {
case SECONDS:
return time.plusSeconds(value);
case MINUTES:
return time.plusMinutes(value);
case HOURS:
return time.plusHours(value);
default:
return time;
}
}
}
public enum ChronoUnitTimeEnum {
SECONDS(ChronoUnit.SECONDS),
MINUTES(ChronoUnit.MINUTES),
HOURS(ChronoUnit.HOURS),
;
private ChronoUnit unit;
private ChronoUnitTimeEnum(ChronoUnit unitType) {
this.unit = unitType;
}
public ChronoUnit getUnit() {
return unit;
}
}
标签: #java时间戳转换日期格式