zoukankan      html  css  js  c++  java
  • java和.net 处理任意格式日期字符串转日期类型,

    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 的区域性特定格式信息。

    返回值

    类型:System.DateTime
    一个对象,它等效于 s 中包含的日期和时间,由 format 和 provider 指定。
     
            private DateTime convToDateTime(string str, string format)
            {
                CultureInfo invariantCulture = System.Globalization.CultureInfo.InvariantCulture;
    
                DateTime dt = DateTime.ParseExact(str, format, invariantCulture);
    
                return dt;
            }
  • 相关阅读:
    Java
    paratest
    ccnet
    资料
    ccnet
    判断类被某个属性应用
    有趣的数学 -- 数学归纳法 -- 互不重叠的单位正方形
    排序算法 -- 堆排序
    APUE CH10 Signals
    APUE CH9 Process Relationship
  • 原文地址:https://www.cnblogs.com/Dylanblogs/p/4267999.html
Copyright © 2011-2022 走看看