首页 > 编程语言 > 基于Calendar获取当前时间的性能比较
2021
09-10

基于Calendar获取当前时间的性能比较

除了获取时间用Date和SimperFormat,还可用Calendar类方法获取时间。

一、Calendar 类常用的获取时间以及时区的方法:

Calendar calendar = Calendar.getInstance();
System.out.println("目前时间: " + calendar.getTime());
System.out.println("Calendar时区: " + calendar.getTimeZone().getID());
System.out.println("user.timezone: " + System.getProperty("user.timezone"));
System.out.println("user.country: " + System.getProperty("user.country"));
System.out.println("默认时区: " + TimeZone.getDefault().getID());

运行结果:

目前时间: Tue May 28 23:09:31 CST 2019
Calendar时区: Asia/Shanghai
user.timezone: Asia/Shanghai
user.country: CN
默认时区: Asia/Shanghai

二、获取当前时间性能比较

long start1 = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(sdf.format(new Date()));// new Date()为获取当前系统时间
long end1 = System.currentTimeMillis();
System.out.println((end1 - start1) + "ms");

运行结果:

2019-05-28 23:14:14
55ms

long start2 = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();//可以对每个时间域单独修改
System.out.println(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND));
long end2 = System.currentTimeMillis();
System.out.println((end2 - start2) + "ms");

运行结果:

2019-4-28 23:15:22
36ms

显然,第二种的Calendar获取当前时间的性能比SimpleDateFormat的要快

三、获取零点时间的性能比较

long start3 = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date zero = calendar.getTime();
long end3 = System.currentTimeMillis();
System.out.println(zero);
System.out.println((end3 - start3) + "ms");

运行结果:

Tue May 28 00:00:00 CST 2019
34ms

long start4 = System.currentTimeMillis();
long current = System.currentTimeMillis();
long zero = current/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
System.out.println(zero);
long end4 = System.currentTimeMillis();
System.out.println((end4 - start4) + "ms");

运行结果:

1558972800000
11ms

这里current表示毫秒,那么除以了1000 * 3600 * 24得到天数,但是可能不是整数天,但是因为t是long型,那么小数部分没有了,再去乘以1000 * 3600 * 24,就变成整数天数所对应的毫秒了。

TimeZone.getDefault().getRawOffset() 计算夏令时和返回当前时区与格林尼治时间的偏差。

显然,这里获取零点时间强烈推荐第二种方法,性能比第一种高三倍左右。

Calendar获取当前年份、月份、日期

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestDate {
    /**
     * 获取当前年份、月份、日期  
     * @param args
     */
    public static void main(String[] args) {
        Calendar cale = null;  
        cale = Calendar.getInstance();  
        int year = cale.get(Calendar.YEAR);  
        int month = cale.get(Calendar.MONTH) + 1;  
        int day = cale.get(Calendar.DATE);  
        int hour = cale.get(Calendar.HOUR_OF_DAY);  
        int minute = cale.get(Calendar.MINUTE);  
        int second = cale.get(Calendar.SECOND);  
        int dow = cale.get(Calendar.DAY_OF_WEEK);  
        int dom = cale.get(Calendar.DAY_OF_MONTH);  
        int doy = cale.get(Calendar.DAY_OF_YEAR);  
  
        System.out.println("Current Date: " + cale.getTime());  
        System.out.println("Year: " + year);  
        System.out.println("Month: " + month);  
        System.out.println("Day: " + day);  
        System.out.println("Hour: " + hour);  
        System.out.println("Minute: " + minute);  
        System.out.println("Second: " + second);  
        System.out.println("Day of Week: " + dow);  
        System.out.println("Day of Month: " + dom);  
        System.out.println("Day of Year: " + doy);  
  
        // 获取当月第一天和最后一天  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
        String firstday, lastday;  
        // 获取前月的第一天  
        cale = Calendar.getInstance();  
        cale.add(Calendar.MONTH, 0);  
        cale.set(Calendar.DAY_OF_MONTH, 1);  
        firstday = format.format(cale.getTime());  
        // 获取前月的最后一天  
        cale = Calendar.getInstance();  
        cale.add(Calendar.MONTH, 1);  
        cale.set(Calendar.DAY_OF_MONTH, 0);  
        lastday = format.format(cale.getTime());  
        System.out.println("本月第一天和最后一天分别是 : " + firstday + " and " + lastday);  
  
        // 获取当前日期字符串  
        Date d = new Date();  
        System.out.println("当前日期字符串1:" + format.format(d));  
        System.out.println("当前日期字符串2:" + year + "/" + month + "/" + day + " "  
                + hour + ":" + minute + ":" + second);  
    }
}

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自学编程网。

编程技巧