獲取當前時間
方法一:函數
SSS是毫秒
2020-04-24 13:00:27.446
${__time(yyyy-MM-dd HH:mm:ss.SSS,)}
方法二:beanshell
import java.text.SimpleDateFormat; import java.util.Date; Date d = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); String str = df.format(d); log.info(str); vars.put("now_time", str);
時間運算
時間比較
方法一:compareTo方法
package com.qzcsbj; public class Test { public static void main(String[] args) { String date1 = "2020-01-21"; String date2 = "2020-01-12"; int res = date1.compareTo(date2); // 兩個時間的格式必須一致,否則報錯 if(res > 0) { System.out.println("date1 > date2"); } else if(res == 0) { System.out.println("date1 = date2"); } else if(res < 0) { System.out.println("date1 < date2"); } } } // date1 > date2
方法二:正則替換,然后轉換為long
package com.qzcsbj; public class Test { public static void main(String[] args) throws InterruptedException { // 將時間字符串轉成Long類型進行比較 String dateStr = "2020-04-25 12:00:00.898"; // \s是指空白,包括空格、換行、tab縮進等所有的空白,而\S剛好相反,只要出現空白就匹配 // "."是不會匹配換行的,所有出現有換行匹配的時候,人們就習慣 使用[\s\S]或者[\w\W]這樣的完全通配模式 long longDate = Long.valueOf(dateStr.replaceAll("[-\\s:\\.]","")); System.out.println(longDate); // 20200425120000898 } }
方法三:轉換為long
package com.qzcsbj; import java.util.Calendar; import java.util.Date; public class Test { public static void main(String[] args) throws InterruptedException { Date time = new Date(); Thread.sleep(1000*1); // 休眠1秒 Calendar calendar = Calendar.getInstance(); // 獲取實例 // System.out.println(calendar); calendar.setTime(time); // 設置時間 // System.out.println(calendar); // 時間加、減 calendar.add(Calendar.HOUR,-1); // 小時減1 Date startTime = calendar.getTime(); calendar.add(Calendar.HOUR,2); // 小時加2 Date endTime = calendar.getTime(); System.out.println(time); // Sun Apr 25 10:06:19 CST 2020 System.out.println(startTime); // Sun Apr 25 09:06:19 CST 2020 System.out.println(endTime); // Sun Apr 25 11:06:19 CST 2020 // 時間戳 long timestamp = time.getTime(); long startTimestamp = startTime.getTime(); long endTimestamp = endTime.getTime(); // System.out.println(timestamp); // 1587720673166 // System.out.println(startTimestamp); // 1587717073166 // System.out.println(endTimestamp); // 1587724273166 // 比較大小 System.out.println(timestamp > startTimestamp); // true System.out.println(timestamp > endTimestamp); // false } }
常用時間轉換
package com.qzcsbj; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Test { public static void main(String[] args) throws InterruptedException, ParseException { Date d = new Date(); System.out.println(d); // Fri Apr 25 17:45:14 CST 2020 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); String str = df.format(d); System.out.println(str); // 2020-04-25 17:44:54.365 Date d2 = df.parse(str); System.out.println(d2); // Fri Apr 25 17:45:14 CST 2020 // 獲取時間戳 long t = d.getTime(); System.out.println(t); // 1587807894365 long t2 = d2.getTime(); System.out.println(t); // 1587807894365 String str2="2020-04-25 17:44:54.365"; Date d3 = df.parse(str2); long t3 = d3.getTime(); System.out.println(t3); // 1587807894365 System.out.println("====================="); // 將時間戳轉換為時間 String str3 = "1587807894365"; SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long lt = new Long(str3); System.out.println(lt); // 1587807894365 Date d4 = new Date(lt); System.out.println(d4); // Sat Apr 25 17:44:54 CST 2020 //日期格式化 String str4 = df2.format(d4); System.out.print(str4 + "\n"); // 2020-04-25 17:44:54 Date d5 = df2.parse(str4); System.out.println(d5); // Sat Apr 25 17:44:54 CST 2020 System.out.println("====================="); // 獲取當前時間的時間戳 System.out.println(System.currentTimeMillis()); // 1587721580028 // java計算程序執行時間 long startTime2=System.currentTimeMillis(); //獲取開始時間 Thread.sleep(1000*2); // 休眠2秒 long endTime2=System.currentTimeMillis(); //獲取結束時間 System.out.println("程序運行時間:"+(endTime2-startTime2)+"ms"); // 程序運行時間:2000ms } }
最新原文:https://www.cnblogs.com/uncleyong/p/12768623.html