博客
关于我
Java学习笔记:日期类的使用
阅读量:813 次
发布时间:2019-03-25

本文共 653 字,大约阅读时间需要 2 分钟。

1,获取当前精确时间

使用 `new Date()` 来获取当前系统时间。

2,日期格式化

通过 SimpleDateFormat 类将 Date 实例格式化为字符串。例如:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");String nowTimeStr = sdf.format(nowTime);

3,获取毫秒数

使用 `System.currentTimeMillis()` 获取从1970年1月1日00:00:00到当前时间的毫秒数。以下是一个示例:

// 截取自1970年1月1日 00:00:00到当前系统的时间总毫秒数long nowTimeMillis = System.currentTimeMillis();System.out.println("当前时间总毫秒数为:" + nowTimeMillis);// 统计一个方法耗费的耗时long begin = System.currentTimeMillis();for (int i = 0; i < 1000; i++) {    System.out.println();}long end = System.currentTimeMillis();System.out.println("耗费时间为:" + (end - begin));

4,建议启动垃圾回收机制

调用 `System.gc()` 以优化内存管理和垃圾回收性能。

转载地址:http://ptjyk.baihongyu.com/

你可能感兴趣的文章
poj 3264(简单线段树)
查看>>
Qt笔记——布局管理三件套分割窗口、停靠窗口和堆栈窗口
查看>>
poj 3277 线段树
查看>>
POJ 3349 Snowflake Snow Snowflakes
查看>>
POJ 3411 DFS
查看>>
poj 3422 Kaka's Matrix Travels (费用流 + 拆点)
查看>>
Qt笔记——官方文档全局定义(二)Functions函数
查看>>
POJ 3468 A Simple Problem with Integers
查看>>
poj 3468 A Simple Problem with Integers 降维线段树
查看>>
poj 3468 A Simple Problem with Integers(线段树 插线问线)
查看>>
poj 3485 区间选点
查看>>
poj 3518 Prime Gap
查看>>
poj 3539 Elevator——同余类bfs
查看>>
Qt笔记——官方文档全局定义(三)Macros宏
查看>>
poj 3628 Bookshelf 2
查看>>
Qt笔记——官方文档全局定义(一)Types数据类型
查看>>
POJ 3670 DP LIS?
查看>>
POJ 3683 Priest John's Busiest Day (算竞进阶习题)
查看>>
POJ 3988 Selecting courses
查看>>
POJ 4020 NEERC John's inversion 贪心+归并求逆序对
查看>>