如何将string转换为datetime
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将string转换为datetime相关的知识,希望对你有一定的参考价值。
java代码:
String strDate = "2017-03-27T13:11:50.657";
strDate = strDate.replaceAll("T", " ");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date dateTime;
try
dateTime = formatter.parse(strDate);
System.out.println(formatter.format(dateTime));
catch (ParseException e)
// TODO Auto-generated catch block
e.printStackTrace();
资料拓展:
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论
参考技术A (1) Date转换为Datetime Datetime在java数据库中对应的是Timestamp,转换如下: [java] view plaincopyprint? //java.util.Date转换为Timestamp Date date = new Date(); Timestamp timestamp = new 参考技术B (1)Date转换为Datetime
Datetime在java数据库中对应的是Timestamp,转换如下:
[java] view
plaincopyprint?
//java.util.Date转换为Timestamp
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime()); //2013-01-14 22:45:36.484
//java.util.Date转换为Timestamp
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime()); //2013-01-14 22:45:36.484
(2) String转换为Date
[java] view
plaincopyprint?
//String转换为java.util.Date
String str = "2013-01-14";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null; //初始化date
try
date = sdf.parse(str); //Mon Jan 14 00:00:00 CST 2013
catch (ParseException e)
e.printStackTrace();
//String转换为java.util.Date
String str = "2013-01-14";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null; //初始化date
try
date = sdf.parse(str); //Mon Jan 14 00:00:00 CST 2013
catch (ParseException e)
e.printStackTrace();
(3)
Date转换为String
[java] view
plaincopyprint?
//java.util.Date转换为String
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String str = format.format(date); //2013-01-14
//java.util.Date转换为String
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String str = format.format(date); //2013-01-14
(4)
String转化为java.sql.Date
转换为java.sql.Date目的是要对数据库进行操作
[java] view
plaincopyprint?
//String转换为java.sql.Date
String str = "2013-01-14";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date sdate = null; //初始化
try
java.util.Date udate = sdf.parse(str);
sdate = new java.sql.Date(udate.getTime()); //2013-01-14
catch (ParseException e)
e.printStackTrace();
本回答被提问者采纳
如何将string 类型字段转换为datetime类型字段
string time = Convert。ToDateTime(this.Birthday.Text);是否转化正确,为什么这样写?
DateTime time = new DateTime();if (!DateTime.TyrParse(this.Birthday.Text, out time))
throw new Exception("无法转换日期!");
//time就是this.Birthday.Text字符转换成的日期类数据 参考技术A DateTime time = Convert.ToDateTime(this.Birthday.Text); 参考技术B 错了吧
DateTime time = Convert.ToDateTime(this.Birthday.Text);
Birthday是什么?textBox? 参考技术C this.Birthday.Text=Convent.ToString(time);
DateTime time = Convert.ToDateTime(this.Birthday.Text);
convent.ToDateTime()是转换日期格式的。
convent.ToString()是转换字符串格式
以上是关于如何将string转换为datetime的主要内容,如果未能解决你的问题,请参考以下文章