• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php如何实现date转string

php 搞代码 4年前 (2022-01-04) 23次浏览 已收录 0个评论

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

Date与String格式的转换

(推荐教程:php视频教程)

SimpleDateFormat.format把date转成String。

SimpleDateFormat.parse把String转成date。

代码测试:

SimpleDateFormat format1 = new SimpleDateForm<strong style="color:transparent">来源gao@daima#com搞(%代@#码网</strong>at("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的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php如何实现date转string

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址