美文网首页
各种类型的转换

各种类型的转换

作者: goule1994 | 来源:发表于2017-06-06 15:04 被阅读23次

    转化过程中用的的二方包

            <!--fastjson -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.32</version>
            </dependency>
    
    Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 
    JSONObject parseObject(String text); // 把JSON文本parse成JSONObject 
    T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean 
    JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
    List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合 
    String toJSONString(Object object); // 将JavaBean序列化为JSON文本 
    String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 
    Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray
    

    String -> Date

    转date代码

            String dateStr = "2010/05/04 12:34:23";   
            Date date = new Date();   
            //注意format的格式要与日期String的格式相匹配   
            DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");   
            try {   
                date = sdf.parse(dateStr);      
            } catch (Exception e) {   
                e.printStackTrace();   
            }  
    

    String -> TimeStamp

            Timestamp ts = new Timestamp(System.currentTimeMillis());   
            String tsStr = "2011-05-09 11:49:45";   
            try {   
                ts = Timestamp.valueOf(tsStr);   
                System.out.println(ts);   
            } catch (Exception e) {   
                e.printStackTrace();   
            }  
    

    TimeStamp -> String

    Timestamp ts = new Timestamp(System.currentTimeMillis());   
            String tsStr = "";   
            DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");   
            try {   
                //方法一   
                tsStr = sdf.format(ts);   
                System.out.println(tsStr);   
                //方法二   
                tsStr = ts.toString();   
                System.out.println(tsStr);   
            } catch (Exception e) {   
                e.printStackTrace();   
            } 
    

    Date -> Timestamp

    new Timestamp((new Date()).getTime())  
    

    Timestamp -> Date

                Timestamp t = new Timestamp(System.currentTimeMillis()); 
                Date d = new Date(t.getTime());  
    

    相关文章

      网友评论

          本文标题:各种类型的转换

          本文链接:https://www.haomeiwen.com/subject/nlggfxtx.html