Java 获取当前日期时间字符串速度测试2306221507
测试代码
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import org.junit.jupiter.api.Test;
public class 获取当前日期时间字符串速度测试2306221507 {
static final int 万 = 10000
,
max = 10*万;
static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static void pln(Object...os) {for(Object o:os)System.out.println(o);}
String st;
// @Test void a预热() { method001(); method002(); method003(); method004Calendar(); }
@Test void method001LocalDateTime() {
for(int i=0; i<max; i++) {
st = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
pln(st);
}
@Test void method001LocalDateTime_2() {
for(int i=0; i<max; i++) {
st = LocalDateTime.now().format(dateTimeFormatter);
}
pln(st);
}
@Test void method002Zdt() {
for(int i=0; i<max; i++) {
st = Zdt.now().yyyy_MM_dd__HH_mm_ss();
}
pln(st);
}
@Test void method003Zdtt() {
for(int i=0; i<max; i++) {
st = new Zdtt().toString();
}
pln(st);
}
@Test void method004Calendar() {
for(int i=0; i<max; i++) {
Calendar cal = Calendar.getInstance();
st = ""+cal.get(Calendar.YEAR) +"-"+ cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH)
+ " "
+cal.get(Calendar.HOUR)+ ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
}
pln(st);
}
@Test void method005UtilDate() {
for(int i=0; i<max; i++) {
st = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
}
pln(st);
}
@Test void method005UtilDate_2() {
for(int i=0; i<max; i++) {
st = simpleDateFormat.format(new java.util.Date());
}
pln(st);
}
@Test void method006SqlDateTime() {
for(int i=0; i<max; i++) {
st = new java.sql.Date(System.currentTimeMillis()).toString()
+ " "
+ new java.sql.Time(System.currentTimeMillis());
}
pln(st);
}
@Test void method007SqlTimestamp() {
for(int i=0; i<max; i++) {
st = new java.sql.Timestamp(System.currentTimeMillis()).toString();
}
pln(st);
}
@Test void method007SqlTimestamp_2() {
for(int i=0; i<max; i++) {
st = new java.sql.Timestamp(System.currentTimeMillis()).toString().substring(0, 19);
}
pln(st);
}
@Test void method007SqlTimestamp_3() {
for(int i=0; i<max; i++) {
st = simpleDateFormat.format(new java.sql.Timestamp(System.currentTimeMillis()));
}
pln(st);
}
@SuppressWarnings("deprecation")
public static class Zdt extends java.util.Date{
/**
* System.currentTimeMillis()返回来自操作系统的时间, 它并不管时区, 如果操作系统设置为本地,它也就是本地
*/
private static final long serialVersionUID = 7810651682610771465L;
public String yyyy , yy , MM , dd , HH , mm , ss ;
public int year , month , day , hour , minute , second;
{
year = super.getYear()+1900;
month = super.getMonth() + 1;
day = super.getDate();
hour = super.getHours();
minute = super.getMinutes();
second = super.getSeconds();
yyyy = String.valueOf(year); yy = yyyy.substring(2);
MM = (month>9) ? String.valueOf(month) : ("0"+month);
dd = day>9 ? String.valueOf(day) : "0"+day ;
HH = hour>9 ? String.valueOf(hour) : "0" +hour;
mm = minute>9 ? String.valueOf(minute) : "0"+minute;
ss = second>9 ? String.valueOf(second) : "0"+second;
}
public Zdt() {super();}
public Zdt(long time) {super(time);}
public String yyyy_MM_dd__HH_mm_ss() {return yyyy+"-"+MM+"-"+dd+" "+HH+":"+mm+":"+ss;}
public String yyyy_MM_dd() {return yyyy+"-"+MM+"-"+dd;}
public String HH_mm_ss() {return HH+":"+mm+":"+ss;}
@Override
public String toString() { return yyyy_MM_dd__HH_mm_ss(); }
public String supperToString() { return super.toString(); }
public static Zdt now() {return new Zdt();}
public static String nowDt() {return now().yyyy_MM_dd();}
public static String nowTm() {return now().HH_mm_ss();}
public static String nowDtt() {return now().yyyy_MM_dd__HH_mm_ss();}
}
/**
*
* @author Kfepiza
* 主要用于System.currentTimeMillis()快速拆分为年月日时分秒毫秒
* 创建速度比java.util.Date快3倍 , 取值更改快
* 1900至2000不考虑夏令时与LocalDateTime结果验证一致
* 2000至2099与LocalDateTime结果验证一致
*/
public static class Zdtt {
final static long T1900 = -2209017600000L ;
final static long T1970 = -28800000L ;
final static long BaseTime = T1900 ;
final static long _1Day = 86400000L;
final static long _365 = _1Day*365L;
final static long Year4 = _1Day*(365*4+1);
long fastTime;
boolean gregorian;
int year , month , dayOfYear , dayOfMonth , hour , minute , second , ms;
String YYYY , MM , DD , HH , mm , ss;
private static String i22(int i) {String r=String.valueOf(i); if(r.length()==1)r="0"+r; return r;}
public Zdtt(long t) {
fastTime = t; ms= t>=0 ? (int) (t%1000) : (int)(10000-(-t%1000))%1000;
long minusBase=t-BaseTime;
long yearCount4 = minusBase/Year4;
long minusLast4 = minusBase%Year4;
long yearCount1 = minusLast4/_365;
long minusLast1 = minusLast4%_365;
long yearMinusBase =yearCount4*4 + yearCount1;
year = (int) (yearMinusBase) + 1900; YYYY=String.valueOf(year); if(YYYY.length()==3)YYYY="0"+YYYY; if(YYYY.length()==2)YYYY="00"+YYYY;
gregorian = isGregorianLeapYear(year);
int dayTemp = dayOfYear = (int) (minusLast1/_1Day)+1;
if(yearCount1==4) {month=1; if(minusLast1<_1Day)dayOfMonth=1; else dayOfMonth=2;}
else {
if(gregorian)dayTemp++;
if(dayTemp<32) {month=1; dayOfMonth=dayTemp;}
else {
if(dayTemp<60) {month=2; dayOfMonth=dayTemp-31;}
else {
if(gregorian)dayTemp--;
if(dayTemp==59) {month=2; dayOfMonth=29; }
else if(dayTemp<91) {month=3; dayOfMonth=dayTemp-59; }
else if(dayTemp<121) {month=4; dayOfMonth=dayTemp-90; }
else if(dayTemp<152) {month=5; dayOfMonth=dayTemp-120; }
else if(dayTemp<182) {month=6; dayOfMonth=dayTemp-151; }
else if(dayTemp<213) {month=7; dayOfMonth=dayTemp-181; }
else if(dayTemp<244) {month=8; dayOfMonth=dayTemp-212; }
else if(dayTemp<274) {month=9; dayOfMonth=dayTemp-243; }
else if(dayTemp<305) {month=10; dayOfMonth=dayTemp-273; }
else if(dayTemp<335) {month=11; dayOfMonth=dayTemp-304; }
else if(dayTemp<366) {month=12; dayOfMonth=dayTemp-334; }
}
}
}
MM = i22(month);
DD = i22(dayOfMonth);
long hmsms = minusLast1%_1Day;
hour = (int) (hmsms / 3600000); HH=i22(hour);
int msms = (int) (hmsms - hour*3600000);
minute = msms / 60000; mm=i22(minute);
int sms = msms - minute*60000;
second = sms / 1000; ss=i22(second);
}
public Zdtt() {this(System.currentTimeMillis());}
public String toString() {return YYYY+"-"+MM+"-"+DD+" "+HH+":"+mm+":"+ss; }
public final boolean isGregorianLeapYear() {return gregorian;}
public static final boolean isGregorianLeapYear(int gregorianYear) {
return (((gregorianYear % 4) == 0)
&& (((gregorianYear % 100) != 0) || ((gregorianYear % 400) == 0))
);
}
}
}
运行在Eclipse2023-06,jdk17.07上
10万次耗时
100万次耗时
1000万次耗时
结论
java.sql.Timestamp
是最快的new SimpleDateFormat
费时费力
© 版权声明
文章版权归作者所有,未经允许请勿转载,侵权请联系 admin@trc20.tw 删除。
THE END