Java編寫時(shí)間工具類ZTDateTimeUtil的示例代碼
1.返回指定格式的當(dāng)前時(shí)間,Date-->FormatString,Date類型轉(zhuǎn)Strig
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String datestring = simpleDateFormat.format(date);
System.out.printf(datestring);2.返回固定格式的Date類型時(shí)間Date---》ToString---》ToDate,Date類型格式化成Date
public static String TimeFormat ="yyyy-MM-dd";
public static void main(String[] args) {
Date date =new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TimeFormat);
String dateformat=simpleDateFormat.format(date);
System.out.printf(dateformat);
Date dateafterFormat =null;
try {
dateafterFormat=simpleDateFormat.parse(dateformat);
System.out.printf(String.valueOf(dateafterFormat));
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
3.字符串轉(zhuǎn)日期 String格式化成String
//parse(String text, ParsePosition pos)
//解析字符串中的文本以Date生成,parase需要背用try-catch包圍
String dateTime ="2023-01-16 17:35:16";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date =simpleDateFormat.parse(dateTime);
System.out.printf(date.toString());
} catch (ParseException e) {
throw new RuntimeException(e);
}
4.兩時(shí)間關(guān)系判斷構(gòu)件
//時(shí)間相等返回0,time1大于time2返回1,time1小于time2返回-1
Date time1 = new Date(System.currentTimeMillis());
String date ="2023-01-17 17:35:16";
Date time2 = null;
try {
time2 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
int flag =time1.compareTo(time2);
System.out.printf(flag+"");
5.Date轉(zhuǎn)換為字符串:Date格式化成String
public static String date2Str(Date date, String format) {
if (null == date) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
6.String類型時(shí)間戳格式化成日期格式String
public static String changesStringTimestamToformat(String timestamp){
LocalDateTime parse = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(timestamp)), ZoneId.systemDefault());
String format = parse.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
return format;
}知識(shí)補(bǔ)充
時(shí)間格式化工具類:時(shí)間格式化工具,一秒前,一分鐘前,一小時(shí)前,昨天,一天前
package com.awifi.cloudnative.container.rbac.user.provider.utils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @Author 張?chǎng)?
* @Date 2022/4/20
* @Param
* @return
**/
public class dataFormatUtils {
/**
* 時(shí)間格式化
*
* @param date
* @return
*/
public static String format(Date date) {
// 計(jì)算出相差天數(shù)
int days = differentDays(date, new Date());
// 同一天
if (0 == days) {
// 計(jì)算出時(shí)間差
long delta = new Date().getTime() - date.getTime();
// 小于一分鐘
if (delta < 1L * 60000L) {
long seconds = toSeconds(delta);
return (seconds <= 0 ? 1 : seconds) + "秒前";
}
// 小于一小時(shí)
else if (delta < 1L * 3600000L) {
long minutes = toMinutes(delta);
return (minutes <= 0 ? 1 : minutes) + "分鐘前";
}
// 小于24小時(shí)
else if (delta < 24L * 3600000L) {
long hours = toHours(delta);
return (hours <= 0 ? 1 : hours) + "小時(shí)前";
}
}
// 不同一天
else {
if (1 == days) {
return "昨天";
}
// 幾天前
else if (3 >= days) {
return days + "天前";
}
}
// 格式化時(shí)間
return getYmdHm(date);
}
/**
* 獲取秒
*
* @param date
* @return
*/
private static long toSeconds(long date) {
return date / 1000L;
}
/**
* 獲取分鐘
*
* @param date
* @return
*/
private static long toMinutes(long date) {
return toSeconds(date) / 60L;
}
/**
* 獲取小時(shí)
*
* @param date
* @return
*/
private static long toHours(long date) {
return toMinutes(date) / 60L;
}
/**
* 時(shí)間格式化,yyyy-MM-dd HH:mm
*/
public static String getYmdHm(Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return df.format(date);
}
/**
* 獲取天數(shù)
*
* @param oldDate
* @param newDate
* @return
*/
public static int differentDays(Date oldDate, Date newDate) {
Calendar oldCal = Calendar.getInstance();
oldCal.setTime(oldDate);
Calendar newCal = Calendar.getInstance();
newCal.setTime(newDate);
int oldDay = oldCal.get(Calendar.DAY_OF_YEAR);
int newDay = newCal.get(Calendar.DAY_OF_YEAR);
int oldYear = oldCal.get(Calendar.YEAR);
int newYear = newCal.get(Calendar.YEAR);
// 不是同一年
if (oldYear != newYear) {
int timeDistance = 0;
for (int i = oldYear; i < newYear; i++) {
//如果是閏年
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
timeDistance += 366;
}
//不是閏年
else {
timeDistance += 365;
}
}
return timeDistance + (newDay - oldDay);
}
// 是同一年
else {
return newDay - oldDay;
}
}
}到此這篇關(guān)于Java編寫時(shí)間工具類ZTDateTimeUtil的示例代碼的文章就介紹到這了,更多相關(guān)Java時(shí)間工具類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot Maven升級(jí)自帶的jar包版本問題
這篇文章主要介紹了SpringBoot Maven升級(jí)自帶的jar包版本問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java實(shí)現(xiàn)byte[]轉(zhuǎn)List的示例代碼
byte,即字節(jié),由8位的二進(jìn)制組成。在Java中,byte類型的數(shù)據(jù)是8位帶符號(hào)的二進(jìn)制數(shù)。List?是一個(gè)接口,它繼承于Collection的接口。它代表著有序的隊(duì)列。本文將介紹如何通過java實(shí)現(xiàn)byte[]轉(zhuǎn)List,需要的可以參考一下2022-01-01
Java Web使用Html5 FormData實(shí)現(xiàn)多文件上傳功能
這篇文章主要介紹了Java Web使用Html5 FormData實(shí)現(xiàn)多文件上傳功能,需要的朋友可以參考下2017-07-07
詳解Spring Cloud Gateway基于服務(wù)發(fā)現(xiàn)的默認(rèn)路由規(guī)則
這篇文章主要介紹了詳解Spring Cloud Gateway基于服務(wù)發(fā)現(xiàn)的默認(rèn)路由規(guī)則,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-05-05
Spring實(shí)現(xiàn)聲明式事務(wù)的方法詳解
這篇文章主要介紹了Spring實(shí)現(xiàn)聲明式事務(wù)的方法詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
MyBatis 中使用 Mapper 簡(jiǎn)化代碼的方法
這篇文章主要介紹了MyBatis 中使用 Mapper 簡(jiǎn)化代碼的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java縮略圖生成庫(kù)之Thumbnailator應(yīng)用說(shuō)明
Thumbnailator是一個(gè)為Java界面更流暢的縮略圖生成庫(kù),從API提供現(xiàn)有的圖像文件和圖像對(duì)象的縮略圖中簡(jiǎn)化了縮略過程,兩三行代碼就能夠從現(xiàn)有圖片生成縮略圖,使用起來(lái)非常方便,需要的朋友可以了解下2012-12-12

