public static int timestrtosec(String time) {
if (Strings.isNullOrEmpty(time)) {
return 0;
}
System.setProperty("user.timezone","GMT +08");
String format = "yyyyMMddHHmmss";
DateFormat dateFormat1 = new SimpleDateFormat(format, Locale.US);
try {
return (int) (dateFormat1.parse(time).getTime() / 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
// timeZoneOffset 为时区
public static String timeStamp2Date(String seconds, String format, int timeZoneOffset) {
if (Strings.isNullOrEmpty(seconds) || seconds.equals("null")) {
return "";
}
if (format == null || format.isEmpty())
format = "yyyyMMddHH";
if (timeZoneOffset > 13 || timeZoneOffset < -12) {
timeZoneOffset = 0;
}
TimeZone timeZone;
String[] ids = TimeZone.getAvailableIDs(timeZoneOffset * 60 * 60 * 1000);
if (ids.length == 0) {
timeZone = TimeZone.getDefault();
} else {
timeZone = new SimpleTimeZone(timeZoneOffset * 60 * 60 * 1000, ids[0]);
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(timeZone);
return sdf.format(new Date(Long.valueOf(seconds + "000")));
}