怎么把string转换datetime
1个回答
展开全部
(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("-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("-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("-MM-dd");
String str = format.format(date); //2013-01-14
//java.util.Date转换为String
Date date = new Date();
DateFormat format = new SimpleDateFormat("-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("-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("-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("-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("-MM-dd");
String str = format.format(date); //2013-01-14
//java.util.Date转换为String
Date date = new Date();
DateFormat format = new SimpleDateFormat("-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("-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();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询