字符串类型的时间和时间类型的相互转换

public static Date getDate(String sDate) throws Exception{
Date date=new Date(); 

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");    //要转换的格式
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=sDate;

//sDate为传入的字符串,如果不为null,则直接转换,parse方法。如果为null,则获取当前时间的前一天并且,

                //拼接字符串" 23:30:00",然后将拼接好的字符串,用parse转换,

if(sDate == null || sDate.length() == 0){
    Calendar calendar = Calendar.getInstance();  
            calendar.setTime(date);  
            calendar.add(Calendar.DAY_OF_MONTH, -1);  
            date = calendar.getTime();
            calendar.getTimeInMillis();
            time = sdf.format(date)+" 23:30:00";      
}                
date = sdf2.parse(time);                
            return date;

}

猜你喜欢

转载自blog.csdn.net/qq_37535558/article/details/81094226