亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java中字符串與日期轉(zhuǎn)換常見方法總結(jié)

 更新時間:2023年11月16日 09:10:38   作者:先知-旭日東升  
這篇文章主要給大家介紹了關(guān)于Java中字符串與日期轉(zhuǎn)換常見方法的相關(guān)資料,在Java編程中經(jīng)常需要將字符串表示的日期轉(zhuǎn)換為日期對象進(jìn)行處理,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

1.Date轉(zhuǎn)String

1.1Date->String

    //date->String
    Date date = new Date();
    String format = dateFormat.format(date);
    System.out.println("format = " + format);

1.2String->Date

    //yyyy-MM-dd HH:mm:ss
    //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String time = "2023-04-03";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    //1.string->date
    Date parse = dateFormat.parse(time);
    System.out.println("parse = " + parse);

2.Date轉(zhuǎn)TimeStamp

2.1Date->TimeStamp

    //Date->TimeStamp
    Date date = new Date();
    long time = date.getTime();
    Timestamp createTime = new Timestamp(time);
    System.out.println("createTime = " + createTime);

2.2TimeStamp->Date

    //TimeStamp->Date
    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    Date timestampToDate = new Date(timestamp.getTime());
    System.out.println("timestampToDate = " + timestampToDate);

3.Date轉(zhuǎn)DateTime

DateTime使用依賴

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.1</version>
    </dependency>

3.1Date->DateTime

方法1:

    //method1
    Date date = new Date();
    DateTime dateTime1 = new DateTime(date);

方法2:

     //method2
    Date date = new Date();
    String dateTimeString = new DateTime(date).toString("yyyy-MM-dd");
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
    DateTime time = dateTimeFormatter.parseDateTime(dateTimeString);
    System.out.println("Date->DateTime: " + time);

3.2DateTime->Date

    //DateTime-&gt;Date
    DateTime dateTime = new DateTime();
    Date dateToDateTime = dateTime.toDate();
    System.out.println("DateTime-&gt;Date" + dateToDateTime);

4.String轉(zhuǎn)DateTime

    //String->DateTime
    String dateTimeString = "2023-04-08";
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
    DateTime time = dateTimeFormatter.parseDateTime(dateTimeString);
    System.out.println("String->DateTime: " + time);
    //DateTime->String
    DateTime dt=new DateTime();
    String format="YYYY-MM-dd HH-mm-ss";
    String str= dt.toString(format);
    System.out.println("DateTime->String = " + str);

5.String與TimeStamp互轉(zhuǎn)

     String timeStr = "2023-04-06 10:30:40";
    //String -> Timestamp
    Timestamp time = Timestamp.valueOf(timeStr);
    //Timestamp -> String
    String strn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
    System.out.println("Timestamp time = " + time);
    System.out.println("strn = " + strn);

總結(jié) 

到此這篇關(guān)于Java中字符串與日期轉(zhuǎn)換常見方法總結(jié)的文章就介紹到這了,更多相關(guān)Java字符串與日期轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論