日期格式多種多樣,代碼實現常用的數種轉換
/**
*
*/
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* @author zhuce
*
*/
public class PaiChu {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("start");
dateFormat("2017/1/10 19:00");
System.out.println("end");
}
public static String dateFormat(final String dateString) {
String string=dateString.replace("/", "-");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
string = simpleDateFormat.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:mm");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d H:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd H:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d H:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d H:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd H:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d HH:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d H:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d H:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d H:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d HH:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd H:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d HH:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd H:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d H:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(dateString + " line=" + Thread.currentThread().getStackTrace()[1].getLineNumber());
return string;
}
}