Java獲取當(dāng)前時(shí)間并轉(zhuǎn)化為yyyy-MM-dd?HH:mm:ss格式的多種方式
方法一(線程不安全, 不建議使用)
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置日期格式
Date now = new Date();
String time = sdf.format(now);方法二(線程安全,建議使用)
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class testMain {
public static void main(String[] args) {
// yyyy-MM-dd HH:mm:ss.SSS ---> 年-月-日 時(shí)-分-秒-毫秒 (想刪掉哪個(gè)小部分就直接刪掉哪個(gè)小部分)
String timeStr1=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String timeStr2=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
System.out.println("當(dāng)前時(shí)間為:"+timeStr1);
System.out.println("當(dāng)前時(shí)間為:"+timeStr2);
}
}
運(yùn)行結(jié)果:
當(dāng)前時(shí)間為:2018-11-27 10:41:47
當(dāng)前時(shí)間為:2018-11-27 10:41:47.392
時(shí)間轉(zhuǎn)時(shí)間戳:
/*
* 將時(shí)間轉(zhuǎn)換為時(shí)間戳
*/
public static String dateToStamp(String s) throws ParseException{
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}/*
* 將時(shí)間戳轉(zhuǎn)換為時(shí)間
*/
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test2 {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(new Date()));
//獲取當(dāng)前時(shí)間戳,也可以是你自已給的一個(gè)隨機(jī)的或是別人給你的時(shí)間戳(一定是long型的數(shù)據(jù))
long timeStamp = System.currentTimeMillis();
//這個(gè)是你要轉(zhuǎn)成后的時(shí)間的格式
SimpleDateFormat sdff=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 時(shí)間戳轉(zhuǎn)換成時(shí)間
String sd = sdff.format(new Date(timeStamp));
System.out.println(sd);//打印出你要的時(shí)間
}
/*
* 將時(shí)間轉(zhuǎn)換為時(shí)間戳
*/
public static String dateToStamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/*
* 將時(shí)間戳轉(zhuǎn)換為時(shí)間
*/
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
} @Test
public void test1(){
/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(new Date()));*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
System.out.println(sdf.format(new Date()));
long currentTimeMillis = System.currentTimeMillis();
System.out.println(currentTimeMillis);
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf2.format(new Date(currentTimeMillis));
System.out.println(format);
}到此這篇關(guān)于Java獲取當(dāng)前時(shí)間并轉(zhuǎn)化為yyyy-MM-dd HH:mm:ss格式的文章就介紹到這了,更多相關(guān)java獲取當(dāng)前時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java.lang.NoClassDefFoundError錯(cuò)誤解決辦法
這篇文章主要介紹了java.lang.NoClassDefFoundError錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
SpringBoot詳解如果通過(guò)@Value注解給靜態(tài)變量注入值
這篇文章主要介紹了springboot如何通過(guò)@Value給靜態(tài)變量注入值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
SpringCloud中Zuul網(wǎng)關(guān)原理及其配置
Spring?Cloud是一個(gè)基于Spring?Boot實(shí)現(xiàn)的微服務(wù)應(yīng)用開發(fā)工具,其中的Zuul網(wǎng)關(guān)可以實(shí)現(xiàn)負(fù)載均衡、路由轉(zhuǎn)發(fā)、鑒權(quán)、限流等功能,本文將從Spring?Cloud中Zuul網(wǎng)關(guān)的原理、使用場(chǎng)景和配置過(guò)程詳細(xì)介紹,幫助大家更好地了解和應(yīng)用Zuul網(wǎng)關(guān),需要的朋友可以參考下2023-06-06
Spring Boot 使用 WebServiceTemplate 調(diào)用&nbs
文章介紹了使用Spring WebServiceTemplate調(diào)用WebService的方法,包括添加依賴、配置WebServiceTemplate、編寫調(diào)用工具類、注意事項(xiàng)及完整的調(diào)用步驟,特別強(qiáng)調(diào)了調(diào)用.NET的WebService需要添加SOAPAction請(qǐng)求頭,感興趣的朋友跟隨小編一起看看吧2026-03-03
使用Java實(shí)現(xiàn)為PPT(PowerPoint)設(shè)置背景
在日益數(shù)字化的辦公環(huán)境中,PowerPoint 演示文稿已成為信息傳達(dá)不可或缺的工具,本文將深入探討如何利用 Spire.Presentation for Java 為PowerPoint幻燈片設(shè)置純色、漸變色和圖片背景,感興趣的可以了解下2025-08-08

