// String to Long
SimpleDateFormat format = new SimpleDateFormat(pattern);
Long time = 0L;
try {
time = format.parse(str).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
// String to Date
DateFormat format = new SimpleDateFormat(pattern);
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
// Long to Date
Date date1 = new Date(1547136000000L);
// Date to Long
Date date2 = new Date();
Long time = date2.getTime();
// Long to String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String str = sdf.format(date);
// Date to String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String str = sdf.format(time);
//获取现在Date:Fri Jan 11 15:22:50 CST 2019
1、 Date currentTime = new Date();
2、 long time = System.currentTimeMillis();
Date date = new Date(time);
/**
* 计算两个时间相差的天数
*/
public static int differentDay(Long startTime, Long endTime) {
return (int) ((endTime - startTime) / (24 * 60 * 60 * 1000L));
}
public static int differentDay(Date date1, Date date2) {
return (int) ((date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000L));
}
public static int differentDay(String str1, String str2, String pattern) throws Exception {
SimpleDateFormat format = new SimpleDateFormat(pattern);
return (int) ((format.parse(str2).getTime() - format.parse(str1).getTime()) / (24 * 60 * 60 * 1000L));
}
/**
* 获取两个日期之间的的所有日期 数组
*/
public static List<Long> listBetweenDays(Long startTime, Long endTime) {
int count = differentDay(startTime, endTime) + 1;
List<Long> list = new ArrayList<>();
Long time = startTime;
for (int i = 0; i < count; i++) {
list.add(time);
time += (24*60*60*1000L);
}
return list;
}