java怎么将date类型转换成datetime类型
2017-06-22 · 做品质教育,用事实说话
日期内容的字符串转化为DateTime类型,将字符类型的日期转化为DateTime类型主要有以下方法:
方法一:Convert.ToDateTime(string)
string格式有要求,必须是yyyy-MM-dd hh:mm:ss
方法二:Convert.ToDateTime(string, IFormatProvider)
DateTime dt;
DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();
dtFormat.ShortDatePattern = "yyyy/MM/dd";
dt = Convert.ToDateTime("2011/05/26", dtFormat);
方法三:DateTime.ParseExact()
string dateString = "20110526";
DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd",System.Globalization.CultureInfo.CurrentCulture);
DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd",System.Globalization.CultureInfo.InvariantCulture);