System.currentTimeMillis()的使用方法

本文通过对比String与StringBuffer在大量字符串拼接时的性能表现,使用System.currentTimeMillis()记录并比较两者运行时间,揭示了不同字符串操作方式对程序效率的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

System.currentTimeMillis()用于获取当前系统时间,以毫秒为单位

获取程序开始时间及结束时间,二者之差即为程序运行时间;

以下是关String与StringBuffer的运行时间比较(辣鸡代码,初学……)

public static void main(String[] args) {
		
		long startTime1=System.currentTimeMillis();
		System.out.println("程序开始时间为:"+startTime1+"毫秒");
		String s1="1";
		for(int i=2;i<100000;i++){
			s1+=String.valueOf(i);
		}
		long endTime1=System.currentTimeMillis();
		System.out.println("程序结束时间为:"+endTime1+"毫秒");
		System.out.println("String的运行时间为:"+(endTime1-startTime1)+"毫秒");
		
		long startTime2=System.currentTimeMillis();
		System.out.println("程序开始时间为:"+startTime2+"毫秒");
		StringBuffer s2=new StringBuffer("1");
		for(int i=2;i<100000;i++){
			s2.append(String.valueOf(i));
		}
		long endTime2=System.currentTimeMillis();
		System.out.println("程序结束时间为:"+endTime2+"毫秒");
		System.out.println("StringBuffer的运行时间为:"+(endTime2-startTime2)+"毫秒");
		
		
	}

附上关于时间转换:

1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)
1秒=1,000,000 微秒(μs) 1微秒=1/1,000,000秒(s)
1秒=1,000,000,000 纳秒(ns) 1纳秒=1/1,000,000,000秒(s)
1秒=1,000,000,000,000 皮秒(ps) 1皮秒=1/1,000,000,000,000秒(s)

1分钟=60秒

1小时=60分钟=3600秒

 //获得系统的时间,单位为毫秒,转换为秒
		long totalMillTimes=System.currentTimeMillis();
		long totalSeconds=totalMillTimes/1000;
		
		//求当前时间的秒
		long totalSecond=totalSeconds%60;
		
		//求当前时间的分
		long totalMinutes=totalSeconds/60;
		long totalMinute=totalMinutes%60;
		
		//求当前时间的时
		long totalHours=totalSeconds/3600;
		long totalHour=totalHours%24;
		
		System.out.println("当前时间为:"+totalMillTimes+"毫秒");
		System.out.println("当前时间为:"+totalHour+":"+totalMinute+":"+totalSecond);

 

### Java 中 `System.currentTimeMillis()` 方法的用法 `System.currentTimeMillis()` 是 Java 提供的一个静态方法,用于获取当前时间与 GMT 时间(格林尼治标准时间)1970 年 1 月 1 日 00:00:00 所相差的毫秒数[^1]。此方法返回的是一个 `long` 类型的数值。 以下是该方法的一些重要特性及其应用: #### 特性描述 - **功能**: 返回从 1970 年 1 月 1 日午夜(UTC/GMT 的起点)到现在的毫秒数。 - **语法**: ```java public static long currentTimeMillis() ``` - **返回值**: 长整型 (`long`) 值表示的时间戳。 --- ### 示例代码展示 以下是一些常见的使用场景以及对应的代码示例: #### 场景一:打印当前时间的时间戳 通过调用 `System.currentTimeMillis()` 可以直接获得当前时间的时间戳并将其打印出来。 ```java public class CurrentTimestampExample { public static void main(String[] args) { long timestamp = System.currentTimeMillis(); System.out.println("当前时间的时间戳:" + timestamp); } } ``` #### 场景二:计算程序运行耗时 可以利用两次调用 `System.currentTimeMillis()` 来测量一段代码执行所需的时间。 ```java public class ExecutionTimeExample { public static void main(String[] args) { long startTime = System.currentTimeMillis(); // 模拟一些操作 for (int i = 0; i <= 10000000; i++) {} long endTime = System.currentTimeMillis(); System.out.println("程序运行耗时:" + (endTime - startTime) + " 毫秒"); } } ``` #### 场景三:将时间戳转换为可读日期格式 可以通过 `SimpleDateFormat` 将时间戳转化为人类易读的形式。 ```java import java.text.SimpleDateFormat; import java.util.Date; public class TimestampToDateExample { public static void main(String[] args) { long timestamp = System.currentTimeMillis(); Date date = new Date(timestamp); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(date); System.out.println("当前时间为:" + formattedDate); } } ``` --- ### 总结 `System.currentTimeMillis()` 主要用来获取当前时间的时间戳,在性能测试、日志记录以及其他需要精确计时的应用场合非常有用。它返回的结果是以毫秒为单位的长整型数据,因此非常适合于高精度的时间运算和比较[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值