zoukankan      html  css  js  c++  java
  • java 获取当前系统时间

    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经典编程题50道之二十九
    Java经典编程题50道之二十八
    Java经典编程题50道之二十七
    Java经典编程题50道之二十六
    Java经典编程题50道之二十五
    Python3爬虫(十一) 爬虫与反爬虫
    Python3爬虫(十) 数据存储之非关系型数据库MongoDB
    Python3爬虫(九) 数据存储之关系型数据库MySQL
    Python3爬虫(八) 数据存储之TXT、JSON、CSV
    Python3爬虫(六) 解析库的使用之Beautiful Soup
  • 原文地址:https://www.cnblogs.com/hong2016/p/7447220.html
Copyright © 2011-2022 走看看