时间工具类,此工具类不是网上大家用烂的那一份,是博主亲自编写,亲自测试,代码简洁清晰,可满足日常开发。
方法
getTime(): 毫秒转字符串
getCurrentTimeInLong(): 获取当前时间(毫秒)
getCurrentTimeInString(): 当前毫秒转字符串
代码
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created on 2021/4/2 11:45
*
* @author Gong Youqiang
*/
public class TimeUtils {
public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
private TimeUtils() {
throw new AssertionError();
}
/**
* long time to string
*
* @param timeInMillis timeInMillis
* @param dateFormat dateFormat
* @return String
*/
public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
return dateFormat.format(new Date(timeInMillis));
}
/**
* long time to string, format is {@link #DEFAULT_DATE_FORMAT}
*
* @param timeInMillis time
* @return String
*/
public static String getTime(long timeInMillis) {
return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
}
/**
* get current time in milliseconds
*
* @return long
*/
public static long getCurrentTimeInLong() {
return System.currentTimeMillis();
}
/**
* get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}
*
* @return String
*/
public static String getCurrentTimeInString() {
return getTime(getCurrentTimeInLong());
}
/**
* get current time in milliseconds
*
* @param dateFormat dateFormat
* @return String
*/
public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
return getTime(getCurrentTimeInLong(), dateFormat);
}
}
网友评论