Java獲取當(dāng)前時間并轉(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 ---> 年-月-日 時-分-秒-毫秒 (想刪掉哪個小部分就直接刪掉哪個小部分)
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)前時間為:"+timeStr1);
System.out.println("當(dāng)前時間為:"+timeStr2);
}
}
運行結(jié)果:
當(dāng)前時間為:2018-11-27 10:41:47
當(dāng)前時間為:2018-11-27 10:41:47.392
時間轉(zhuǎn)時間戳:
/*
* 將時間轉(zhuǎn)換為時間戳
*/
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;
}/*
* 將時間戳轉(zhuǎn)換為時間
*/
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)前時間戳,也可以是你自已給的一個隨機的或是別人給你的時間戳(一定是long型的數(shù)據(jù))
long timeStamp = System.currentTimeMillis();
//這個是你要轉(zhuǎn)成后的時間的格式
SimpleDateFormat sdff=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 時間戳轉(zhuǎn)換成時間
String sd = sdff.format(new Date(timeStamp));
System.out.println(sd);//打印出你要的時間
}
/*
* 將時間轉(zhuǎn)換為時間戳
*/
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;
}
/*
* 將時間戳轉(zhuǎn)換為時間
*/
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)前時間并轉(zhuǎn)化為yyyy-MM-dd HH:mm:ss格式的文章就介紹到這了,更多相關(guān)java獲取當(dāng)前時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?數(shù)據(jù)交換?Json?和?異步請求?Ajax詳解
Json(JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,采用鍵值對的形式來表示數(shù)據(jù),它廣泛應(yīng)用于Web開發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲,這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請求Ajax,需要的朋友可以參考下2023-09-09
Mybatis?在?insert?插入操作后返回主鍵?id的操作方法
這篇文章主要介紹了Mybatis?在?insert?插入操作后返回主鍵?id的操作方法,本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
Java 客戶端操作 FastDFS 實現(xiàn)文件上傳下載替換刪除功能
這篇文章主要介紹了Java 客戶端操作 FastDFS 實現(xiàn)文件上傳下載替換刪除功能,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
JavaWeb項目中dll文件動態(tài)加載方法解析(詳細步驟)
這篇文章主要介紹了JavaWeb項目中dll文件動態(tài)加載方法,步驟詳細,在這里分享給大家,需要的朋友可以了解下。2017-09-09
Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法
下面小編就為大家?guī)硪黄狫ava 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

