常用时间工具类

常用时间工具类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author: Lambert
* @create: 2019-12-29 22:09
*/
public class TimeUtils {
/**
* 获取当前微妙时间戳方法
*
* @return
*/
public static Long getmicTime() {
// 微秒
Long cutime = System.currentTimeMillis() * 1000;
// 纳秒
Long nanoTime = System.nanoTime();
return cutime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
}

/**
* 过去几天前时间戳(微妙)
* @param day
* @return
* @throws ParseException
*/
public static Long pasttime(Long day) {
// 时间格式化
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date as = new Date(System.currentTimeMillis() - (24 * day) * 60 * 60 * 1000);
long time = as.getTime();
// 微秒
Long cutime = time * 1000;
// 纳秒
Long nanoTime = System.nanoTime();
return cutime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
}

/**
* 时间转时间戳(毫秒)
* @param s
* @return
* @throws ParseException
*/
public static String dateToStamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
System.out.println(res);
return res;
}

}

猜你喜欢

转载自www.cnblogs.com/lizhen1412/p/12116942.html
今日推荐