1. 程式人生 > >JAVA中時間與字串的相互轉換(工具類)

JAVA中時間與字串的相互轉換(工具類)

<span style="font-size:24px;">//model為字串的時間格式,如"<span style="font-family: arial; line-height: 20.02px;">yy-MM-dd</span><span style="font-family: arial; line-height: 20.02px;"> HH:mm:ss"</span></span>
<span style="font-size:24px;">public class DateFormat {
    public static Date StringToDate(String date,String model){	//字串轉時間
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(model);
        Date date1=null;
        try {
            date1=simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }

    public static String DateToString(Date date,String model){	//時間轉字串
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(model);
        return simpleDateFormat.format(date);
    }
}</span>