C#字串轉換為日期時間格式及其錯誤處理
文章關鍵字: c#字串轉換為日期 c#日期轉換字串 字串轉換日期 字串轉換為date 整數轉換為字串 浮點數轉換為字串 字串轉換為時間 將字串轉換為時間 字元轉換為字串 字串轉換為時間 物件 字串轉換為日期型 字串轉換成日期
√字串轉換為日期型(C#)
如:"20100101"轉換成日期型?
"20100101"轉換成int型怎麼轉換??
1、DateTime dt=Convert.ToDateTime("20100101".Substring(0,4)+"-"+"20100101".Substring(4,2)+"-"+"20071107".Substring(6,2));
int i=Convert.ToInt32("20100101");
2、Convert.ToDateTime、DateTime.Parse()
3、stringstr="20100101";
DateTime dt=DateTime.ParseExact(str,"yyyyMMdd",null);
inti;
int.TryParse(str,outi);
4、定義一個DateTimePicker物件,然後將需要轉化的字串賦給這個DateTimePicker物件的Text屬性,
然後DateTimePicker物件的Value值就是你需要的日期和時間,Value值還有Minite,Second等屬性,可以取得
時,分,秒,豪秒等值.
√JAVA字串轉換成日期時間格式
//日期時間格式:yyyy-MM-dd hh:mm:ss
String time ="1900-02-21 12:23:33";
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//生成的日期和時間
Date date = dateformat .parse(time);
date就是我們想要的
如果程式中有2個或2個以上的地方呼叫了dateformat.parse()方法,則有可能會報錯:Unhandled exception type ParseException。
為此,需要利用try-catch塊來捕捉:
try{
dateformat .parse(time);
} catch(Exception e) {
e.printStackTrace();
}