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?數(shù)據(jù)交換?Json?和?異步請(qǐng)求?Ajax詳解
Json(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,采用鍵值對(duì)的形式來(lái)表示數(shù)據(jù),它廣泛應(yīng)用于Web開(kāi)發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲(chǔ),這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請(qǐng)求Ajax,需要的朋友可以參考下2023-09-09MyBatis-Plus解決邏輯刪除與唯一索引的問(wèn)題
本文主要介紹了MyBatis-Plus解決邏輯刪除與唯一索引的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Mybatis?在?insert?插入操作后返回主鍵?id的操作方法
這篇文章主要介紹了Mybatis?在?insert?插入操作后返回主鍵?id的操作方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12Java 客戶端操作 FastDFS 實(shí)現(xiàn)文件上傳下載替換刪除功能
這篇文章主要介紹了Java 客戶端操作 FastDFS 實(shí)現(xiàn)文件上傳下載替換刪除功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10JavaWeb項(xiàng)目中dll文件動(dòng)態(tài)加載方法解析(詳細(xì)步驟)
這篇文章主要介紹了JavaWeb項(xiàng)目中dll文件動(dòng)態(tài)加載方法,步驟詳細(xì),在這里分享給大家,需要的朋友可以了解下。2017-09-09Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法
下面小編就為大家?guī)?lái)一篇Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02