Java的Date获取时间函数都是deprecated

可以使用:

https://stackoverflow.com/questions/5175728/how-to-get-the-current-date-time-in-java

所说方法

It depends on what form of date / time you want:

  • If you want the date / time as a single numeric value, then System.currentTimeMillis() gives you that, expressed as the number of milliseconds after the UNIX epoch (as a Java long). This value is a delta from a UTC time-point, and is independent of the local time-zone ... assuming that the system clock has been set correctly.

  • If you want the date / time in a form that allows you to access the components (year, month, etc) numerically, you could use one of the following:

    • new Date() gives you a Date object initialized with the current date / time. The problem is that the Date API methods are mostly flawed ... and deprecated.

    • Calendar.getInstance() gives you a Calendar object initialized with the current date / time, using the default Locale and TimeZone. Other overloads allow you to use a specific Locale and/or TimeZone. Calendar works ... but the APIs are still cumbersome.

    • new org.joda.time.DateTime() gives you a Joda-time object initialized with the current date / time, using the default time zone and chronology. There are lots of other Joda alternatives ... too many to describe here.

    • in Java 8, calling LocalDateTime.now() and ZonedDateTime.now() will give you representations for the current date / time.

Prior to Java 8, most people who know about these things recommended Joda-time as having (by far) the best Java APIs for doing things involving time point and duration calculations. With Java 8, this is no longer true. However, if you are already using Joda time in your codebase, there is no strong reason to migrate.

date和datetime转换

https://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date

Date in = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());
Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());

 

java 获取当前系统时间的更多相关文章

  1. Java 获取当前系统时间方法比较

    转载: http://blog.csdn.net/zzjjiandan/article/details/8372617 一. 获取当前系统时间和日期并格式化输出: import java.util.D ...

  2. JAVA获取当前系统时间System.currentTimeMillis()

    System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMi ...

  3. JAVA获取当前系统时间System.currentTimeMillis()以及获取运行时间

    System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMi ...

  4. java获取当前系统时间

    阿里巴巴推荐 Timestamp d = new Timestamp(System.currentTimeMillis()); 唯一的好处就是除了Timestamp,没有再新建什么了

  5. JAVA中获取当前系统时间及格式转换

    JAVA中获取当前系统时间   一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...

  6. JAVA中获取当前系统时间

    一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowStrin ...

  7. 关于Java中获取当前系统时间

    一. 获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleDateFormat; public class NowStri ...

  8. java/python中获取当前系统时间,并与字符串相互转换格式,或者转化成秒数,天数等整数

    java转换成秒数 Date类有一个getTime()可以换回秒数,例如: public class DateToSecond { public static void main(String[] a ...

  9. java 获取当前系统系时间

    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 SimpleDateFo ...

随机推荐

  1. Linux arm64的虚拟内存布局

    原创翻译,转载请注明出处. 页表转换arm64在硬件体系结构上支持4级的每页大小为4K的页表转换,也支持3级的页大小64KB的页表转换.在linux arm64中,如果页的大小为4KB,使用3级页表转 ...

  2. 微信小程序小程序使用scroll-view不能使用下拉刷新的解决办法

    <scroll-view class="movie-grid-container" scroll-y="true" scroll-x="fals ...

  3. Aspose.words 替换字符 操作

    var path = Server.MapPath("~/doc/demo.doc"); Document doc = new Document(path); DocumentBu ...

  4. [Java] 各种常用函数

    1.writeInt()和readInt() 这两个函数并不是写入一个整数,读取一个整数.它们实际上是写入4个字节,读取4个字节. writeInt(int i)把i按四个字节,二进制形式写到输出流里 ...

  5. 猜数字(C语言版)

    编程先由计算机“想”一个1到100之间的数请人猜,如果人猜对了,则结束游戏,并在屏幕上输出人猜了多少次才猜对此数,以此来反映猜数者“猜”的水平,否则计算机给出提示,告诉人所猜的数是太大还是太小,最多可 ...

  6. Reabble.com-KindleRSS新闻杂志订阅

    Reabble.com-KindleRSS新闻杂志订阅 TreeInsertions Tomcat部署war包后,运行时出现如下错误 RectangularCovering Reabble.com-K ...

  7. [洛谷P3567][POI2014]KUR-Couriers

    题目大意:给一个数列,每次询问一个区间内有没有一个数出现次数超过一半.有,输出这个数,否则输出$0$ 题解:主席树,查询区间第$\bigg\lfloor\dfrac{len+1}{2}\bigg\rf ...

  8. http get post 参数校验

    spring boot 常见http get ,post请求参数处理   在定义一个Rest接口时通常会利用GET.POST.PUT.DELETE来实现数据的增删改查:这几种方式有的需要传递参数,后台 ...

  9. angular js 模拟获取后台的数据

    在这里我们把后台的数据用一个.json文件进行代替. 项目的目录结构如下: puDongLibraryLearning----ui-router-learning ---- data-------pe ...

  10. sysctl -P net.bridge.bridge-nf-call-ip6tables报错解决办法

    问题症状 修改 linux 内核文件 #vi /etc/sysctl.conf后执行sysctl  -P 报错 error: "net.bridge.bridge-nf-call-ip6ta ...