* java Data、String、Long三种日期类型之间的相互转换,需要的拿去用*
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by wyw on 2017/5/11.
*/
public class MyDateUtils {
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
public static String longToString(long currentTime, String formatType)
throws ParseException {
Date date = longToDate(currentTime, formatType);
String strTime = dateToString(date, formatType);
return strTime;
}
public static Date stringToDate(String strTime, String formatType)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
public static Date longToDate(long currentTime, String formatType)
throws ParseException {
Date dateOld = new Date(currentTime);
String sDateTime = dateToString(dateOld, formatType);
Date date = stringToDate(sDateTime, formatType);
return date;
}
public static long stringToLong(String strTime, String formatType)
throws ParseException {
Date date = stringToDate(strTime, formatType);
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date);
return currentTime;
}
}
public static long dateToLong(Date date) {
return date.getTime();
}
}