将当前日期转换为整数

Bat*_*rai 28 java date

我想将当前日期转换为整数形式.默认情况下,它返回long.当我尝试将long转换为整数,然后我将整数值转换为日期意味着它显示1970年的日期.

 int i = (int) new Date().getTime();
 System.out.println("Integer : " + i);
 System.out.println("Long : "+ new Date().getTime());
 System.out.println("Long date : " + new Date(new Date().getTime()));
 System.out.println("Int Date : " + new Date(i));
Run Code Online (Sandbox Code Playgroud)

输出将成为

Integer : 1292838124
Long : 1345617601771
Long date : Wed Aug 22 12:10:01 IST 2012
Int Date : Fri Jan 16 04:37:18 IST 1970
Run Code Online (Sandbox Code Playgroud)

任何人请帮助我,如何将当前日期转换为整数(10位数字)

sho*_*ser 45

问题是Integer不足以存储当前日期,您需要使用Long.

日期在内部存储为自1970年1月1日以来的毫秒数.

最大整数值为2147483648,而自1970年以来的毫秒数目前为1345618537869

将最大整数值放入日期会产生1970年1月26日星期一.

编辑:根据以下评论显示除以1000的代码:

    int i = (int) (new Date().getTime()/1000);
    System.out.println("Integer : " + i);
    System.out.println("Long : "+ new Date().getTime());
    System.out.println("Long date : " + new Date(new Date().getTime()));
    System.out.println("Int Date : " + new Date(((long)i)*1000L));

Integer : 1345619256
Long : 1345619256308
Long date : Wed Aug 22 16:37:36 CST 2012
Int Date : Wed Aug 22 16:37:36 CST 2012
Run Code Online (Sandbox Code Playgroud)

  • 如果你愿意以一秒的分辨率生存(你将失去日期的毫秒成分),那么你可以在存储整数之前除以1000.然后乘以转换回来.然而,它的寿命有限(一种Y2K问题) - 高达2038年. (11认同)

Bas*_*que 10

太长了;博士

\n\n
Instant.now().getEpochSecond()  // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.\n
Run Code Online (Sandbox Code Playgroud)\n\n

细节

\n\n

正如其他人所说,32 位整数无法容纳从纪元(UTC 1970 年初)到现在的秒数。您需要64 位整数(long基元或Long对象)。

\n\n

java.time

\n\n

其他答案是使用旧的遗留日期时间类。它们已被 java.time 类取代。

\n\n

该类表示UTCInstant时间线上的一个时刻,分辨率为纳秒

\n\n
Instant now = instant.now() ;\n
Run Code Online (Sandbox Code Playgroud)\n\n

您可以询问自纪元以来的毫秒数。请注意,这意味着数据丢失,将纳秒截断为毫秒。

\n\n
long millisecondsSinceEpoch = now.toEpochMilli() ;\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您想计算自纪元以来的纳秒数,您将需要做一些数学计算,因为该类奇怪地缺少toEpochNano方法。请注意L十亿后面附加的 ,以引发作为 64 位长整数的计算。

\n\n
long nanosecondsSinceEpoch = ( instant.getEpochSecond() * 1_000_000_000L ) + instant.getNano() ;\n
Run Code Online (Sandbox Code Playgroud)\n\n

自纪元以来的整秒

\n\n

但问题末尾要求输入 10 位数字。我怀疑这意味着自 1970-01-01T00:00:00 纪元以来的整秒数。这通常称为Unix 时间或 Posix 时间。

\n\n

我们可以询问Instant这个号码。同样,这意味着该对象可能保存的任何秒数都会被截断,从而导致数据丢失。

\n\n
long secondsSinceEpoch = now.getEpochSecond() ;  // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

关于java.time

\n\n

java.time框架内置于 Java 8 及更高版本中这些类取代了麻烦的旧遗留日期时间类,例如java.util.Date, Calendar, &SimpleDateFormat

\n\n

Joda -Time项目现在处于维护模式,建议迁移到java.time类。

\n\n

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

\n\n

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

\n\n

从哪里获取 java.time 类?

\n\n\n\n

ThreeTen -Extra项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如Interval、、YearWeekYearQuarter

\n


小智 8

为了将当前日期作为整数(10位数字),您需要将从新Date().getTime()返回的long除以1000.

这将在int范围内,并且一直持续到2038年1月18日.


Rud*_*udy 5

如果您只需要一个表示从1970年1月1日起经过的天数的整数,则可以尝试以下操作:

// magic number=
// millisec * sec * min * hours
// 1000 * 60 * 60 * 24 = 86400000
public static final long MAGIC=86400000L;

public int DateToDays (Date date){
    //  convert a date to an integer and back again
    long currentTime=date.getTime();
    currentTime=currentTime/MAGIC;
    return (int) currentTime; 
}

public Date DaysToDate(int days) {
    //  convert integer back again to a date
    long currentTime=(long) days*MAGIC;
    return new Date(currentTime);
}
Run Code Online (Sandbox Code Playgroud)

较短但可读性较差(速度稍快?):

public static final long MAGIC=86400000L;

public int DateToDays (Date date){
    return (int) (date.getTime()/MAGIC);
}

public Date DaysToDate(int days) {
    return new Date((long) days*MAGIC);
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。

编辑:这可能直到周五Jul 07 01:00:00 CET 5881580