字符串与时间格式的相互操作

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class DateUtils {

public static Date strToDateYYYYMMDD(String str){
if(str == null)return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}

public static Date strToDateYYYYMMDDHHMMDD(String str){
if(str == null)return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}

public static String dateToStr(Date date){
if(date == null)return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}

public static String dateToStr(Date date,boolean chinese){
if(date == null)return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
return sdf.format(date);
}


// public static void main(String[] args){
// System.out.println(strToDateYYYYMMDD("2000-10-10"));
// System.out.println(strToDateYYYYMMDDHHMMDD("2000-10-10 12:12:12"));
//
// }
//
}

你可能感兴趣的:(java)