如何将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语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论
展开全部
(1) Date转换为Datetime Datetime在java数据库中对应的是Timestamp,转换如下: [java] view plaincopyprint? //java.util.Date转换为Timestamp Date date = new Date(); Timestamp timestamp = new
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-11-09 · 知道合伙人软件行家
关注
展开全部
(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();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询