java 时间操作
private static final String HOUR_FORMAT = "%02d00";
private static final String HOUR_FORMAT_SCALE = "%.2s:00";
/**
* 获取系统当前时间小时数
*
* @return
*/
public static String getCurHour24() {
Calendar calendar = Calendar.getInstance();
int curHour24 = calendar.get(Calendar.HOUR_OF_DAY);
return String.format(HOUR_FORMAT, curHour24);
}
/**
* j将4位小时数转换为冒号隔开的
* @param hour
* @return
*/
public static String getCurHour24Scale(String hour) {
return String.format(HOUR_FORMAT_SCALE, hour);
}
/**
* 将时间戳转化为时间
*
* @param timestamp
* @return
*/
public static LocalDateTime convertDate(Long timestamp) {
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), TimeZone
.getDefault().toZoneId());
return dateTime;
}
/**
* 获取昨天零点时间戳
*
* @return
*/
public static Long yesterdayStart() {
LocalDateTime yesterdayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).minus(Period.ofDays(1));
return convertMilli(yesterdayStart);
}
/**
* 获取昨天时间
*
* @return
*/
public static LocalDate yesterdayTime() {
LocalDate yesterdayTime = LocalDate.now().minus(Period.ofDays(1));
return yesterdayTime;
}
/**
* 获取昨天结束时间戳
*/
public static Long yesterdayEnd() {
LocalDateTime yesterdayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).minus(Period.ofDays(1));
// DateTimeFormatter time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return convertMilli(yesterdayEnd);
}
/**
* 将时间转化为时间戳
*
* @return
*/
public static Long convertMilli(LocalDateTime dateTime) {
long epochMilli = dateTime.toInstant(ZoneOffset.of("+8")).getEpochSecond();
return epochMilli;
}
/**
* 将时间格式转化为yyyyMMdd
*
* @param time
* @return
*/
public static String convertDate(LocalDate time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
return time.format(formatter);
}
/**
* LocalDate转String
* @param
*/
public static String converString(LocalDate time){
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return time.format(fmt);
}
public static void main(String[] args) {
System.out.println(String.format("%02d00", 9));
}