timestamp有兩個屬性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP兩種,使用情況分别如下:
1.CURRENT_TIMESTAMP
當要向資料庫執行insert操作時,如果有個timestamp字段屬性設為
CURRENT_TIMESTAMP,則無論這個字段有木有set值都插入目前系統時間
2.ON UPDATE CURRENT_TIMESTAMP
1.時間戳轉Date
public static void main(String[] args) {
// 10位的秒級别的時間戳
long time1 = 1527767665;
String result1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time1 * 1000));
System.out.println("10位數的時間戳(秒)--->Date:" + result1);
Date date1 = new Date(time1*1000); //對應的就是時間戳對應的Date
// 13位的秒級别的時間戳
double time2 = 1515730332000d;
String result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time2);
System.out.println("13位數的時間戳(毫秒)--->Date:" + result2);
}
10位數的時間戳(秒)--->Date:2018-05-31 19:54:25
13位數的時間戳(毫秒)--->Date:2018-01-12 12:12:12
2. 判斷兩個時間點時候為同一天,同一年
/**
* 判斷是否為同一天:使用commons-lang包下的DateUtils類
*
* @param day1
* @param day2
* @return
*/
public boolean isSameDay(Date day1, Date day2) {
return DateUtils.isSameDay(day1, day2);
}
/**
* 判斷是否為同一天:使用joda依賴包裡的時間類,效率從一定程度上優于DateUtils.isSameDay()方法
*
* @param date1
* @param date2
* @return
*/
public static boolean isSameDay1(Date date1,Date date2){
if(date1==null || date2==null){
throw new IllegalArgumentException("date must be not null");
}
LocalDate localDate1 = new LocalDate(new DateTime(date1.getTime()));
LocalDate localDate2 = new LocalDate(new DateTime(date2.getTime()));
return localDate1.equals(localDate2);
}
/**
* 格式化輸出
*/
public static String messageFormat(String pattern, Object[] arr) {
if (pattern == null || arr == null) {
return "";
}
return new MessageFormat(pattern).format(arr);
}