最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java獲取當(dāng)前時(shí)間并轉(zhuǎn)化為yyyy-MM-dd?HH:mm:ss格式的多種方式

 更新時(shí)間:2024年03月25日 10:10:15   作者:一蓑煙雨任平生2024  
這篇文章主要介紹了Java獲取當(dāng)前時(shí)間并轉(zhuǎn)化為yyyy-MM-dd?HH:mm:ss格式的多種方式,每種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

方法一(線程不安全, 不建議使用)

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ò)誤解決辦法

    這篇文章主要介紹了java.lang.NoClassDefFoundError錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Mybatis 自定義類型處理器示例詳解

    Mybatis 自定義類型處理器示例詳解

    在某些情況下我們需要對(duì)類型做處理,例如數(shù)據(jù)存儲(chǔ)的是Long,程序里是BigDecimal,那么我們出庫入庫都需要做處理,此時(shí)就可以使用類型處理器,本文通過示例給大家介紹Mybatis 自定義類型處理器的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • SpringBoot詳解如果通過@Value注解給靜態(tài)變量注入值

    SpringBoot詳解如果通過@Value注解給靜態(tài)變量注入值

    這篇文章主要介紹了springboot如何通過@Value給靜態(tài)變量注入值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 深入理解equals和hashCode方法

    深入理解equals和hashCode方法

    在Java中,equals和hashCode方法是Object中提供的兩個(gè)方法,這兩個(gè)方法對(duì)以后的學(xué)習(xí)有很大的幫助,本文就深度來去講解這兩個(gè)方法。下面小編帶大家來一起學(xué)習(xí)吧
    2019-06-06
  • SpringCloud中Zuul網(wǎng)關(guān)原理及其配置

    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)景和配置過程詳細(xì)介紹,幫助大家更好地了解和應(yīng)用Zuul網(wǎng)關(guān),需要的朋友可以參考下
    2023-06-06
  • Java ObjectMapper使用詳解

    Java ObjectMapper使用詳解

    ObjectMapper類是Jackson的主要類,它可以幫助我們快速的進(jìn)行各個(gè)類型和Json類型的相互轉(zhuǎn)換,本文給大家介紹Java ObjectMapper的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • Spring Boot 使用 WebServiceTemplate 調(diào)用 WebService的完整步驟

    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
  • JDK17安裝教程以及其環(huán)境變量配置教程

    JDK17安裝教程以及其環(huán)境變量配置教程

    環(huán)境變量對(duì)Java初學(xué)者來說真的是一件頭疼的事,本人也經(jīng)歷過這樣的事情,這篇文章主要給大家介紹了關(guān)于JDK17安裝教程以及其環(huán)境變量配置的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • Java實(shí)現(xiàn)彈窗效果的基本操作(2)

    Java實(shí)現(xiàn)彈窗效果的基本操作(2)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)彈窗效果的基本操作第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • 使用Java實(shí)現(xiàn)為PPT(PowerPoint)設(shè)置背景

    使用Java實(shí)現(xiàn)為PPT(PowerPoint)設(shè)置背景

    在日益數(shù)字化的辦公環(huán)境中,PowerPoint 演示文稿已成為信息傳達(dá)不可或缺的工具,本文將深入探討如何利用 Spire.Presentation for Java 為PowerPoint幻燈片設(shè)置純色、漸變色和圖片背景,感興趣的可以了解下
    2025-08-08

最新評(píng)論

元谋县| 利辛县| 江华| 铁力市| 屏南县| 荃湾区| 栾川县| 调兵山市| 淳安县| 道孚县| 洞口县| 溧阳市| 乌审旗| 梁平县| 磴口县| 鱼台县| 万州区| 察雅县| 昆山市| 汉源县| 马鞍山市| 盈江县| 扶余县| 铅山县| 乌兰察布市| 鄂伦春自治旗| 故城县| 章丘市| 宁强县| 宜春市| 冀州市| 桐庐县| 磐石市| 武邑县| 定襄县| 孝义市| 蒲城县| 格尔木市| 沅江市| 潼南县| 偏关县|