Java編寫時間工具類ZTDateTimeUtil的示例代碼
1.返回指定格式的當前時間,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類型時間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.兩時間關系判斷構(gòu)件
//時間相等返回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類型時間戳格式化成日期格式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;
}知識補充
時間格式化工具類:時間格式化工具,一秒前,一分鐘前,一小時前,昨天,一天前
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 張鑫
* @Date 2022/4/20
* @Param
* @return
**/
public class dataFormatUtils {
/**
* 時間格式化
*
* @param date
* @return
*/
public static String format(Date date) {
// 計算出相差天數(shù)
int days = differentDays(date, new Date());
// 同一天
if (0 == days) {
// 計算出時間差
long delta = new Date().getTime() - date.getTime();
// 小于一分鐘
if (delta < 1L * 60000L) {
long seconds = toSeconds(delta);
return (seconds <= 0 ? 1 : seconds) + "秒前";
}
// 小于一小時
else if (delta < 1L * 3600000L) {
long minutes = toMinutes(delta);
return (minutes <= 0 ? 1 : minutes) + "分鐘前";
}
// 小于24小時
else if (delta < 24L * 3600000L) {
long hours = toHours(delta);
return (hours <= 0 ? 1 : hours) + "小時前";
}
}
// 不同一天
else {
if (1 == days) {
return "昨天";
}
// 幾天前
else if (3 >= days) {
return days + "天前";
}
}
// 格式化時間
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;
}
/**
* 獲取小時
*
* @param date
* @return
*/
private static long toHours(long date) {
return toMinutes(date) / 60L;
}
/**
* 時間格式化,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;
}
}
}到此這篇關于Java編寫時間工具類ZTDateTimeUtil的示例代碼的文章就介紹到這了,更多相關Java時間工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實現(xiàn)byte[]轉(zhuǎn)List的示例代碼
byte,即字節(jié),由8位的二進制組成。在Java中,byte類型的數(shù)據(jù)是8位帶符號的二進制數(shù)。List?是一個接口,它繼承于Collection的接口。它代表著有序的隊列。本文將介紹如何通過java實現(xiàn)byte[]轉(zhuǎn)List,需要的可以參考一下2022-01-01
Java Web使用Html5 FormData實現(xiàn)多文件上傳功能
這篇文章主要介紹了Java Web使用Html5 FormData實現(xiàn)多文件上傳功能,需要的朋友可以參考下2017-07-07
詳解Spring Cloud Gateway基于服務發(fā)現(xiàn)的默認路由規(guī)則
這篇文章主要介紹了詳解Spring Cloud Gateway基于服務發(fā)現(xiàn)的默認路由規(guī)則,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

