各种日期时间格式化转换
public class StringUtils {
public static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
public static String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static String DEFAULT_TIME_DIVISION = "HH:mm:ss";
public final static String EMPTY = "";
/**
* 格式化日期字符串
*
* @param date
* @param pattern
* @return
*/
public static String formatDate(Date date, String pattern) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(date);
}
/**
* 格式化日期字符串
*
* @param date
* @return 例如2011-3-24
*/
public static String formatDate(Date date) {
return formatDate(date, DEFAULT_DATE_PATTERN);
}
/**
* 获取当前时间 格式为yyyy-MM-dd 例如2011-07-08
*
* @return
*/
public static String getDate() {
return formatDate(new Date(), DEFAULT_DATE_PATTERN);
}
/**
* 获取当前时间
*
* @return
*/
public static String getDateTime() {
return formatDate(new Date(), DEFAULT_DATETIME_PATTERN);
}
/**
* 格式化日期时间字符串
*
* @param date
* @return 例如2011-11-30 16:06:54
*/
public static String formatDateTime(Date date) {
return formatDate(date, DEFAULT_DATETIME_PATTERN);
}
/**
* 获取utc格式的时间
*
* @return
*/
public static String GetUTCTime() {
Calendar cal = Calendar.getInstance(Locale.CHINA);
int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
int dstOffset = cal.get(Calendar.DST_OFFSET);
cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
return DateFormat.format("yyyy'-'MM'-'dd'T'kk':'mm':'ss'", cal).toString();
}
public static String join(final ArrayList<String> array, String separator) {
StringBuffer result = new StringBuffer();
if (array != null && array.size() > 0) {
for (String str : array) {
result.append(str);
result.append(separator);
}
result.delete(result.length() - 1, result.length());
}
return result.toString();
}
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
// date类型转换为String类型
// formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
// data Date类型的时间
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
// long类型转换为String类型
// currentTime要转换的long类型的时间
// formatType要转换的string类型的时间格式
public static String longToString(long currentTime, String formatType) {
if (formatType == null) {
formatType = "HH:mm:ss";
}
Date date = null; // long类型转成Date类型
try {
date = longToDate(currentTime, formatType);
} catch (ParseException e) {
e.printStackTrace();
}
String strTime = dateToString(date, formatType); // date类型转成String
return strTime;
}
// string类型转换为date类型
// strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH时mm分ss秒,
// strTime的时间格式必须要与formatType的时间格式相同
public static Date stringToDate(String strTime, String formatType)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
// long转换为Date类型
// currentTime要转换的long类型的时间
// formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
public static Date longToDate(long currentTime, String formatType)
throws ParseException {
Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
return date;
}
// string类型转换为long类型
// strTime要转换的String类型的时间
// formatType时间格式
// strTime的时间格式和formatType的时间格式必须相同
public static long stringToLong(String strTime, String formatType)
throws ParseException {
Date date = stringToDate(strTime, formatType); // String类型转成date类型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date类型转成long类型
return currentTime;
}
}
// date类型转换为long类型
// date要转换的date类型的时间
public static long dateToLong(Date date) {
return date.getTime();
}
@SuppressLint("SimpleDateFormat")
public static String converTime(String srcTime, TimeZone timezone) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
SimpleDateFormat dspFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String convertTime;
Date result_date;
long result_time = 0;
// 如果传入参数异常,使用本地时间
if (null == srcTime)
result_time = System.currentTimeMillis();
else {
try { // 将输入时间字串转换为UTC时间
sdf.setTimeZone(TimeZone.getTimeZone("GMT00:00"));
result_date = sdf.parse(srcTime);
result_time = result_date.getTime();
} catch (Exception e) { // 出现异常时,使用本地时间
result_time = System.currentTimeMillis();
dspFmt.setTimeZone(TimeZone.getDefault());
convertTime = dspFmt.format(result_time);
return convertTime;
}
}
// 设定时区
dspFmt.setTimeZone(timezone);
convertTime = dspFmt.format(result_time);
return convertTime;
}
}
注:格式:”HH:mm:ss”中HH使用大写是24小时制,使用小写hh是12小时制