Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将日期转换为时间戳会返回错误的日期_Java_Android_Date - Fatal编程技术网

Java 将日期转换为时间戳会返回错误的日期

Java 将日期转换为时间戳会返回错误的日期,java,android,date,Java,Android,Date,我有下面的函数,它应该返回一个时间戳。当使用如下斜杠以字符串格式输入日期时,此代码有效:“2019/3/4”,但在使用新日期(年、月、日)时,此代码无效。 发生了什么?当您调用日期(整数年、整数月、整数日期)时,参数应如下所示: 由于日期(整数年、整数月、整数日期)已被弃用,您可以使用日历和设置来获得所需的日期 Calendar calendar = Calendar.getInstance() calendar.set(2019, Calendar.MARCH, 4) Long timeInM

我有下面的函数,它应该返回一个时间戳。当使用如下斜杠以字符串格式输入日期时,此代码有效:“2019/3/4”,但在使用新日期(年、月、日)时,此代码无效。

发生了什么?

当您调用
日期(整数年、整数月、整数日期)
时,参数应如下所示:

由于
日期(整数年、整数月、整数日期)
已被弃用,您可以使用
日历
设置
来获得所需的
日期

Calendar calendar = Calendar.getInstance()
calendar.set(2019, Calendar.MARCH, 4)
Long timeInMillis = calendar.timeInMillis

// timeInMillis should be 1551718624170 equivalent to Mon Mar 04 2019
tl;博士 如果您希望获取该日期的第一个时刻,如UTC所示,转换为自1970-01-01T00:00Z以来的毫秒计数:

LocalDate             // Represent a date, without time-of-day and without time zone.  
.of( 2019 , 3 , 4 )   // Determine a date. Uses sane numbering, 1-12 for January-December.
.atStartOfDay(        // Determine first moment of the day.
    ZoneOffset.UTC    // Get the first moment as seen at UTC (an offset of zero hours-minutes-days.
)                     // Returns a `ZonedDateTime` object.
.toInstant()          // Convert from a `ZonedDateTime` to a simple `Instant` object, which is always in UTC, and has methods to get count-from-epoch.
.toEpochMilli()       // Get a count of milliseconds since the epoch reference of first moment of 1970 to UTC.
java.time 您正在使用可怕的日期时间类,这些类在几年前被JSR310中定义的现代java.time类所取代

要获取自UTC 1970年第一个时刻(1970-01-01T00:00Z)的历元参考以来的毫秒或整秒计数,请使用

或:

对于日期,您必须确定该日期当天的第一个时刻。这并不总是00:00的时间,所以让java.time来确定这一时刻。这样做需要一个时区。一天在东方比在西方早开始,全球各地的时区不同

LocalDate ld = LocalDate.of( 2019 , 3 , 4 ) ;
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;
Instant instant = zdt.toInstant() ;
long millis = Instant.now().toEpochMilli() ;

关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

该项目现已启动,建议迁移到类

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*

从哪里获得java.time类

  • 、和更高版本-标准Java API的一部分,带有捆绑实现。
    • Java9添加了一些次要功能和修复
    • 大多数java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(遵循Date方法的JavaDoc。它声明第一个参数year的值应该是

      年份减去1900。

      因此调用dateToTimestamp方法的正确版本是

      dateToTimestamp(新日期(119,3,4)

      这将为您提供
      1554316200000
      。转换后的日期为2019年4月4日


      请注意月份的值从0开始表示一月,1开始表示二月,依此类推。

      @不推荐的公共日期(int year,int month,int Date){this(year,month,Date,0,0,0);}这是一个不推荐的方法。也添加你正在获取的错误。尝试<代码> 2019-1990 而不是<代码> 2019代码> @ SHHAAL,因为转换后的值是错误的,我不会在任何地方记录错误。这回答了你的问题吗?我建议。我建议你考虑扔掉长期过时和设计不良的<代码>戴特。
      class并添加到您的Android项目中,以便使用
      java.time
      ,这是一种现代的java日期和时间API。它使用起来非常方便。我当然更强烈地建议您在任何情况下都远离过时的
      date
      类的不推荐的构造函数和方法。@OleV.V.I您在顶部进行编辑,将时区更改为
      ZoneOffset.UTC
      ,因为我试图在顶部显示UTC并在下面进行分区。感谢您的编辑和其他编辑。抱歉,我在那里关注得太少。感谢您的回复。
      Calendar calendar = Calendar.getInstance()
      calendar.set(2019, Calendar.MARCH, 4)
      Long timeInMillis = calendar.timeInMillis
      
      // timeInMillis should be 1551718624170 equivalent to Mon Mar 04 2019
      
      LocalDate             // Represent a date, without time-of-day and without time zone.  
      .of( 2019 , 3 , 4 )   // Determine a date. Uses sane numbering, 1-12 for January-December.
      .atStartOfDay(        // Determine first moment of the day.
          ZoneOffset.UTC    // Get the first moment as seen at UTC (an offset of zero hours-minutes-days.
      )                     // Returns a `ZonedDateTime` object.
      .toInstant()          // Convert from a `ZonedDateTime` to a simple `Instant` object, which is always in UTC, and has methods to get count-from-epoch.
      .toEpochMilli()       // Get a count of milliseconds since the epoch reference of first moment of 1970 to UTC.
      
      long millis = Instant.now().toEpochMilli() ;
      
      long seconds = Instant.now().getEpochSecond() ;
      
      LocalDate ld = LocalDate.of( 2019 , 3 , 4 ) ;
      ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
      ZonedDateTime zdt = ld.atStartOfDay( z ) ;
      Instant instant = zdt.toInstant() ;
      long millis = Instant.now().toEpochMilli() ;