时间格式工具类
public class TimeFormat {
public static long currentDiff(long currentMillis, long preMillis) {
long diff = 0L;
diff = (currentMillis - preMillis) / 1000L;
return diff;
}
public static String millisToDate(long millis) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(millis);
String dateStr = dateformat.format(gc.getTime());
return dateStr;
}
public static String millisToDate(String millis) {
long longMillis = Long.parseLong(millis);
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(longMillis);
String dateStr = dateformat.format(gc.getTime());
return dateStr;
}
public static long dateToMillis(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long milles = 0L;
try {
milles = dateFormat.parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return milles;
}
public static String getCurrentDateString() {
Date day = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = df.format(day);
return dateString;
}
public static Long parse_date(String date, boolean type) {
if (date.equals("0"))
return -1L;
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);
Date d2 = null;
try {
d2 = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat simpleDateFormat;
if (type) {
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
} else {
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
String format = simpleDateFormat.format(d2);
long dateToMillis = dateToMillis(format);
return dateToMillis;
}
public static String format_date(String date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d2 = null;
try {
d2 = simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);
String format = sdf.format(d2);
return format;
}
}```