搜尋
首頁後端開發PHP問題php如何實作date轉string

php如何實作date轉string

Oct 12, 2020 am 09:48 AM
datephpstring

php實作date轉string的方法:可以利用SimpleDateFormat.format把date轉成String,如【str3 = format1.format(date1);】。

php如何實作date轉string

Date與String格式的轉換

(推薦教學:php影片教學

SimpleDateFormat.format把date轉成String。

SimpleDateFormat.parse把String轉成date。

程式碼測試:

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date date1 = null;
Date date2 = null;
String str1 = "2009-02-14 12:00:00";
String str2 = "2009年02月14日 12时00分00秒";

// String转Date:String 必须严格按照定义的格式
try {
date1 = format1.parse(str1);
date2 = format2.parse(str2);
} catch (ParseException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("date1= "+date1);
System.out.println("date2= "+date2);

//Date转StringString 
str3 = null;
String str4 = null;
str3 = format1.format(date1);
str4 = format2.format(date2);
System.out.println("str1= "+str3);
System.out.println("str2= "+str4);

相關推薦:php訓練

#

以上是php如何實作date轉string的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版