1、SimpleDateFormat.parse 把指定格式字符串转日期类型
public static Calendar convToCalender(String str,String template){ SimpleDateFormat sdf; Date date; Calendar cltResult = Calendar.getInstance(); sdf = new SimpleDateFormat(template, Locale.getDefault()); try { date = sdf.parse(str); cltResult.setTime(date); } catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(); } return cltResult; }
2、DateTime.ParseExact 方法 (String s, String format, IFormatProvider provider)
参数
-
s
-
类型:
System.String
包含要转换的日期和时间的字符串。
-
format
-
类型:
System.String
用于定义所需的 s 格式的格式说明符。
-
provider
-
类型:
System.IFormatProvider
一个对象,提供有关 s 的区域性特定格式信息。
private DateTime convToDateTime(string str, string format) { CultureInfo invariantCulture = System.Globalization.CultureInfo.InvariantCulture; DateTime dt = DateTime.ParseExact(str, format, invariantCulture); return dt; }