欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中的SimpleDateFormat使用詳解

 更新時(shí)間:2017年03月30日 14:37:44   投稿:mrr  
SimpleDateFormat 是一個(gè)以國(guó)別敏感的方式格式化和分析數(shù)據(jù)的具體類。這篇文章主要介紹了Java中的SimpleDateFormat使用詳解,需要的朋友可以參考下

public class SimpleDateFormat extends DateFormat

SimpleDateFormat 是一個(gè)以國(guó)別敏感的方式格式化和分析數(shù)據(jù)的具體類。 它允許格式化 (date -> text)、語(yǔ)法分析 (text -> date)和標(biāo)準(zhǔn)化。

SimpleDateFormat 允許以為日期-時(shí)間格式化選擇任何用戶指定的方式啟動(dòng)。 但是,希望用 DateFormat 中的 getTimeInstance、 getDateInstance 或 getDateTimeInstance 創(chuàng)建一個(gè)日期-時(shí)間格式化程序。 每個(gè)類方法返回一個(gè)以缺省格式化方式初始化的日期/時(shí)間格式化程序。 可以根據(jù)需要用 applyPattern 方法修改格式化方式。

SimpleDateFormat函數(shù)的繼承關(guān)系:

Java.lang.Object
  |
  +----java.text.Format
      |
      +----java.text.DateFormat
          |
          +----java.text.SimpleDateFormat

下面是個(gè)小例子:

import java.text.*;
import java.util.Date;
/**
 SimpleDateFormat函數(shù)語(yǔ)法:
 G 年代標(biāo)志符
 y 年
 M 月
 d 日
 h 時(shí) 在上午或下午 (1~12)
 H 時(shí) 在一天中 (0~23)
 m 分
 s 秒
 S 毫秒
 E 星期
 D 一年中的第幾天
 F 一月中第幾個(gè)星期幾
 w 一年中第幾個(gè)星期
 W 一月中第幾個(gè)星期
 a 上午 / 下午 標(biāo)記符 
 k 時(shí) 在一天中 (1~24)
 K 時(shí) 在上午或下午 (0~11)
 z 時(shí)區(qū)
 */
public class FormatDateTime {
  public static void main(String[] args) {
    SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒");
    SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); 
    SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等價(jià)于now.toLocaleString()
    SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒 E ");
    SimpleDateFormat myFmt4=new SimpleDateFormat(
        "一年中的第 D 天 一年中第w個(gè)星期 一月中第W個(gè)星期 在一天中k時(shí) z時(shí)區(qū)");
    Date now=new Date();
    System.out.println(myFmt.format(now));
    System.out.println(myFmt1.format(now));
    System.out.println(myFmt2.format(now));
    System.out.println(myFmt3.format(now));
    System.out.println(myFmt4.format(now));
    System.out.println(now.toGMTString());
    System.out.println(now.toLocaleString());
    System.out.println(now.toString());
  }  
}

效果:

2004年12月16日 17時(shí)24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17時(shí)24分27秒 星期四
一年中的第 351 天 一年中第51個(gè)星期 一月中第3個(gè)星期 在一天中17時(shí) CST時(shí)區(qū)

16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004

下面是個(gè)JavaBean:

public class FormatDateTime {
  public static String toLongDateString(Date dt){
    SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒 E ");    
    return myFmt.format(dt);
  }
  public static String toShortDateString(Date dt){
    SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH時(shí)mm分");    
    return myFmt.format(dt);
  }  
  public static String toLongTimeString(Date dt){
    SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");    
    return myFmt.format(dt);
  }
  public static String toShortTimeString(Date dt){
    SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");    
    return myFmt.format(dt);
  }
  public static void main(String[] args) {
    Date now=new Date();
    System.out.println(FormatDateTime.toLongDateString(now));
    System.out.println(FormatDateTime.toShortDateString(now));
    System.out.println(FormatDateTime.toLongTimeString(now));
    System.out.println(FormatDateTime.toShortTimeString(now));
  }  
}

調(diào)用的main 測(cè)試結(jié)果:

2004年12月16日 17時(shí)38分26秒 星期四
04年12月16日 17時(shí)38分
17 38 26 0965
04/12/16 17:38

以上所述是小編給大家介紹的Java中的SimpleDateFormat使用詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 淺談SpringMVC對(duì)RESTfull的支持

    淺談SpringMVC對(duì)RESTfull的支持

    這篇文章主要介紹了淺談SpringMVC對(duì)RESTfull的支持,分享了相關(guān)配置代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置

    Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置

    這篇文章主要介紹了Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2007-11-11
  • java自定義jar包讓jmeter使用的方法

    java自定義jar包讓jmeter使用的方法

    在本篇文章里小編給大家整理了一篇關(guān)于java自定義jar包讓jmeter使用的方法以及實(shí)例代碼,需要的朋友們參考下。
    2019-10-10
  • java多線程編程之join方法的使用示例

    java多線程編程之join方法的使用示例

    join方法的功能就是使異步執(zhí)行的線程變成同步執(zhí)行。也就是說(shuō),當(dāng)調(diào)用線程實(shí)例的start方法后,這個(gè)方法會(huì)立即返回,如果在調(diào)用start方法后后需要使用一個(gè)由這個(gè)線程計(jì)算得到的值,就必須使用join方法
    2014-01-01
  • java實(shí)現(xiàn)檢測(cè)是否字符串中包含中文

    java實(shí)現(xiàn)檢測(cè)是否字符串中包含中文

    本文給大家分享了2個(gè)使用java檢測(cè)字符串中是否包含中文的代碼,都非常的實(shí)用,最后附上了各種字符的unicode編碼的范圍,方便我們以后使用正則進(jìn)行匹配檢測(cè)。
    2015-10-10
  • 詳細(xì)解讀Druid數(shù)據(jù)庫(kù)連接池的使用

    詳細(xì)解讀Druid數(shù)據(jù)庫(kù)連接池的使用

    這篇文章主要介紹了Druid數(shù)據(jù)庫(kù)連接池的使用,數(shù)據(jù)庫(kù)連接池負(fù)責(zé)分配、管理和釋放數(shù)據(jù)庫(kù)連接,它允許應(yīng)用程序重復(fù)使用一個(gè)現(xiàn)有的數(shù)據(jù)庫(kù)連接,而不是重新建立一個(gè),需要的朋友可以參考下
    2023-03-03
  • springboot讀取nacos配置文件的實(shí)現(xiàn)

    springboot讀取nacos配置文件的實(shí)現(xiàn)

    SpringBoot注冊(cè)服務(wù)到Nacos上,由Nacos來(lái)做服務(wù)的管理,本文主要介紹了springboot讀取nacos配置文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • java:程序包org.springframework.boot不存在的完美解決方法

    java:程序包org.springframework.boot不存在的完美解決方法

    最近項(xiàng)目中運(yùn)行的時(shí)候提示了"java: 程序包org.springframework.boot不存在",下面這篇文章主要給大家介紹了關(guān)于java:程序包org.springframework.boot不存在的完美解決方法,需要的朋友可以參考下
    2023-05-05
  • Java創(chuàng)建數(shù)組的3種方式代碼舉例

    Java創(chuàng)建數(shù)組的3種方式代碼舉例

    數(shù)組是相同類型數(shù)據(jù)的有序集合,數(shù)組描述的是若干個(gè)相同類型的數(shù)據(jù)按照一定的先后次序排列組合而成,其中每一個(gè)數(shù)據(jù)稱為數(shù)組的元素,可以通過(guò)下標(biāo)進(jìn)行訪問(wèn),這篇文章主要給大家介紹了關(guān)于Java創(chuàng)建數(shù)組的3種方式,需要的朋友可以參考下
    2024-01-01
  • SpringBoot啟用GZIP壓縮的代碼工程

    SpringBoot啟用GZIP壓縮的代碼工程

    經(jīng)常我們都會(huì)與服務(wù)端進(jìn)行大數(shù)據(jù)量的文本傳輸,例如?JSON?就是常見(jiàn)的一種格式,通過(guò)?REST?API?接口進(jìn)行?GET?和?POST?請(qǐng)求,可能會(huì)有大量的文本格式數(shù)據(jù)提交、返回,壓縮和解壓在提升網(wǎng)絡(luò)帶寬的同時(shí),會(huì)帶來(lái)?CPU?資源的損耗,本文介紹了SpringBoot啟用GZIP壓縮的代碼工程
    2024-08-08

最新評(píng)論