1.long字符串转换成yyyy-MM-dd HH:mm:ss 格式输出
/**
* 将长时间格式字符串转换为字符串 yyyy-MM-dd HH:mm:ss
* @return
*/
public static String longToStrng(){
String time="1256006105375";//long型转换成的字符串
Date date= new Date(Long.parseLong(time.trim()));
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
System.out.println("TIME:::"+dateString);
return dateString;
}
2、字符串转换成时间
/**
* 将yyyy-MM-dd HH:mm:ss 时间格式字符串转化成时间
* @return
*/
public static Date StringToDate(){
String time="2009-10-21 10:35:05";//时间格式的字符串
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date s=null;
try {
s= formatter.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("TIME:::"+s);
return s;
}
3、取得当前系统时间 返回 yyyy-MM-dd HH:mm:ss字符串
/**
* 获取现在时间
*
* @return返回字符串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
System.out.println("TIME:::"+dateString);
return dateString;
}
4、取得当前系统时间 返回 HH:mm:ss字符串
/**
* 获取时间 小时:分;秒 HH:mm:ss
*
* @return
*/
public static String getTimeShort() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date currentTime = new Date();
String dateString = formatter.format(currentTime);
System.out.println("TIME:::"+dateString);
return dateString;
}
5、将20090402103531 转换成 yyyy-MM-dd HH:mm:ss形式输出
/**
* 将20090402103531 转换成 yyyy-MM-dd HH:mm:ss形式输出
* @return
*/
public static String strToDatestr() {
String ssss = "20090402103531";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sss = null;
try {
sss = sdf2.format(sdf.parse(ssss));
System.out.println("result" + sss);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sss;
}