我想将当前日期转换为整数形式.默认情况下,它返回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));
输出将成为
Integer : 1292838124
Long : 1345617601771
Long date : Wed Aug 22 12:10:01 IST 2012
Int Date : Fri Jan 16 04:37:18 IST 1970
任何人请帮助我,如何将当前日期转换为整数(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
Bas*_*que 10
Instant.now().getEpochSecond()  // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.\n正如其他人所说,32 位整数无法容纳从纪元(UTC 1970 年初)到现在的秒数。您需要64 位整数(long基元或Long对象)。
其他答案是使用旧的遗留日期时间类。它们已被 java.time 类取代。
\n\n该类表示UTCInstant时间线上的一个时刻,分辨率为纳秒。
Instant now = instant.now() ;\n您可以询问自纪元以来的毫秒数。请注意,这意味着数据丢失,将纳秒截断为毫秒。
\n\nlong millisecondsSinceEpoch = now.toEpochMilli() ;\n如果您想计算自纪元以来的纳秒数,您将需要做一些数学计算,因为该类奇怪地缺少toEpochNano方法。请注意L十亿后面附加的 ,以引发作为 64 位长整数的计算。
long nanosecondsSinceEpoch = ( instant.getEpochSecond() * 1_000_000_000L ) + instant.getNano() ;\n但问题末尾要求输入 10 位数字。我怀疑这意味着自 1970-01-01T00:00:00 纪元以来的整秒数。这通常称为Unix 时间或 Posix 时间。
\n\n我们可以询问Instant这个号码。同样,这意味着该对象可能保存的任何秒数都会被截断,从而导致数据丢失。
long secondsSinceEpoch = now.getEpochSecond() ;  // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.\njava.time框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如java.util.Date, Calendar, &SimpleDateFormat。
Joda -Time项目现在处于维护模式,建议迁移到java.time类。
\n\n要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
\n\n您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
从哪里获取 java.time 类?
\n\nThreeTen -Extra项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如Interval、、等YearWeekYearQuarter。
如果您只需要一个表示从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);
}
较短但可读性较差(速度稍快?):
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);
}
希望这可以帮助。
编辑:这可能直到周五Jul 07 01:00:00 CET 5881580
| 归档时间: | 
 | 
| 查看次数: | 175150 次 | 
| 最近记录: |