在JAVA中如何将String转换成Datetime类型
8个回答
展开全部
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语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论
推荐于2017-09-23 · 知道合伙人数码行家
关注
展开全部
(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();
}
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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你说的Datetime应该是数据库中的吧?
Datetime在java类中对应的是Timestamp ,用下面的方法转成Timestamp 就可以往数据库中茶了,但是string的时间格式要符合我写的那种
String s = "2008-06-13 09:25:12";
Timestamp time = Timestamp.valueOf(s);
System.out.println(time);
Datetime在java类中对应的是Timestamp ,用下面的方法转成Timestamp 就可以往数据库中茶了,但是string的时间格式要符合我写的那种
String s = "2008-06-13 09:25:12";
Timestamp time = Timestamp.valueOf(s);
System.out.println(time);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
(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();
}
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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
好似没有可以直接利用的函数,自己编一个函数吧,可以分析其中的某些字符串,然后根据字符串内容赋值给时间。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询