java simpledateformat是什麼?讓我們一起來了解一下吧!
其實,作爲一名Java程序員,會經常在編程時候和時間打交道,比如要把某一個時間存儲到數據庫中去:
Datedate = newDate(); System.out.println(date);
得到的時間是這樣的
SunJun 07 17:22:52CST 2020
因此,需要把時間進行格式化處理,然後再進行存儲方便閱讀。這個時候就會使用到SimpleDateFormat類,比如使用下面的代碼來獲取當前時間,並調用SimpleDateFormat 對時間進行格式化:
Date date = newDate(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設定日期格式String datestr = df.format(date); System.out.println(datestr);
最終輸出的時間爲
2020-06-07 16:45:58
simpledateformat構造方法摘要:
1. simpledateformat():以默認的模式和語言環境構造simpledateformat。
2. simpledateformat(string pattern):以自己設定的模式和語言環境構造simpledateformat。
3. simpledateformat(string pattern,dateformatsymbols):以自己設定的模式和日期符號構造simpledateformat。
java simpledateformat方法具體代碼如下:
package ceshi; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date; public class Ceshi { public static void main(String[] args) throws ParseException { SimpleDateFormat CeshiFmt0=new SimpleDateFormat("Gyyyy年MM月dd日 HH時mm分ss秒"); SimpleDateFormat CeshiFmt1=new SimpleDateFormat("yyyy/MM/dd HH:mm"); SimpleDateFormat CeshiFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat CeshiFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E "); SimpleDateFormat CeshiFmt4=new SimpleDateFormat("yyyy/MM/dd E"); SimpleDateFormat CeshiFmt5=new SimpleDateFormat( "一年中的第 D 天 ,第w個星期 ,一個月中第W個星期 ,k時 z時區"); Date now=new Date(); System.out.println(CeshiFmt0.format(now)); System.out.println(CeshiFmt1.format(now)); System.out.println(CeshiFmt2.format(now)); System.out.println(CeshiFmt3.format(now)); System.out.println(CeshiFmt4.format(now)); System.out.println(CeshiFmt5.format(now)); } }
綜上所述,java simpledateformat是java程序中的關於日期的一種方法。dateformat是關乎時間/日期格式子類的抽象類,它是透過與語言無關的方法去達到格式化且解析日期或者時間的目的。而simpledateformat就是dateformat的子類。