generated from root/miduo_server
108 lines
2.7 KiB
Java
108 lines
2.7 KiB
Java
package com.ljsd.util;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
|
|
|
|
/**
|
|
* 时间的工具类
|
|
*/
|
|
public class TimeUtil {
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(TimeUtil.class);
|
|
|
|
private final static SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
|
|
|
|
public static void sleep(long millis) {
|
|
try {
|
|
Thread.sleep(millis);
|
|
} catch (Exception e) {
|
|
LOGGER.error("sleep->msg=null", e.getMessage(), e);
|
|
}
|
|
}
|
|
|
|
public static boolean inCorrectTime(String startTime, String endTime) throws Exception {
|
|
long now = System.currentTimeMillis();
|
|
long start = sdf.parse(startTime).getTime();
|
|
long end = sdf.parse(endTime).getTime();
|
|
if (now < start) {
|
|
return false;
|
|
}
|
|
if (now > end) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static long fixTime(String fixTime) {
|
|
String[] temp = fixTime.split(":");
|
|
int hour = Integer.parseInt(temp[0]);
|
|
int minute = Integer.parseInt(temp[1]);
|
|
int second = Integer.parseInt(temp[2]);
|
|
|
|
Calendar c = Calendar.getInstance();
|
|
c.set(Calendar.HOUR_OF_DAY, hour);
|
|
c.set(Calendar.MINUTE, minute);
|
|
c.set(Calendar.SECOND, second);
|
|
c.set(Calendar.MILLISECOND, 0);
|
|
return c.getTimeInMillis();
|
|
}
|
|
|
|
/**
|
|
* 获取周几
|
|
*
|
|
* @return
|
|
*/
|
|
public static int getDayOfWeek() {
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTimeInMillis(System.currentTimeMillis());
|
|
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
return (week == 0) ? 7 : week;
|
|
}
|
|
|
|
/**
|
|
* 获取指定时间 周几
|
|
*
|
|
* @return
|
|
*/
|
|
public static int getDayOfWeek(long time) {
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTimeInMillis(time);
|
|
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
return (week == 0) ? 7 : week;
|
|
}
|
|
|
|
/**
|
|
* 字符串 时间格式 转秒时间戳
|
|
* @param date
|
|
* @return
|
|
*/
|
|
public static long timeStrToSecond(String date){
|
|
try {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
return sdf.parse(date).getTime()/1000;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* 获取当前时间,秒
|
|
* @return
|
|
*/
|
|
public static int nowInt(){
|
|
return (int) (System.currentTimeMillis()/1000);
|
|
}
|
|
|
|
/**
|
|
* 获取当前时间,秒
|
|
* @return
|
|
*/
|
|
public static long nowLong(){
|
|
return System.currentTimeMillis();
|
|
}
|
|
} |