String類型轉換成Date類型

@Test
    public void test() throws ParseException {
        //字符串數組
        String[] s1 = new String[] { "8,9", "9,10" };
        for (String string : s1) {
            // 分割出來的字符數組
            String[] sourceStrArray = string.split(",");
            for (String string2 : sourceStrArray) {
                Date date = StringToDate(string2);
                System.out.println(date);
            }
        }
    }


    //字符串轉換成Date
    public static Date StringToDate(String string2) throws ParseException {
        String strDate = "2012-3-1";
        String format2 = "yyyy-MM-dd HH:mm";
        String Time = string2 + ":00";
        SimpleDateFormat sdf = new SimpleDateFormat(format2);
        return sdf.parse(strDate + " " + Time);

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章