1、计算百分比
private String getPercent(long first, long second) {
if (first == 0 || second == 0)
return "0";
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(2);
String result = numberFormat.format((float)first / (float)second * 100) + "%";
return result;
}
2、每天开始时间点
public static Long getStartOfDay(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
3、每天结束时间点
public static Long getEndOfDay(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTimeInMillis();
}
4、获取时间段内的日期
private static List<Date> getDays(long time_start, long time_end) {
List<Date> days = new ArrayList<>();
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date start = dateFormat.parse(dateFormat.format(new Date(time_start).getTime()));
Date end = dateFormat.parse(dateFormat.format(new Date(time_end).getTime()));
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, +1);
while (tempStart.before(tempEnd)) {
days.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (Exception e) {
e.printStackTrace();
}
return days;
}