将当前日期转换为整数 [英] Convert Current date as integer

查看:129
本文介绍了将当前日期转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将当前日期转换为整数形式。默认情况下返回长。当我尝试将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));

输出将变为

 整数:1292838124 
长:1345617601771
长日期:Wed Aug 22 12:10:01 IST 2012
国际日期:Fri Jan 16 04:37:18 IST 1970

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

解决方案

问题是整数不足以存储当前日期,您需要使用Long。 >

日期自内部存储为自1/1/1970以来的毫秒数。



最大整数值为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));

整数:1345619256
长:1345619256308
长日期:Wed Aug 22 16:37:36 CST 2012
Int日期:Wed Aug 22 16:37:36 CST 2012


I wants to convert the current date as integer form. By default it returns long. When I try to convert the long as integer, and then i convert the integer value to date means it shows 1970's date.

 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));

the output will becomes

Integer : 1292838124
Long : 1345617601771
Long date : Wed Aug 22 12:10:01 IST 2012
Int Date : Fri Jan 16 04:37:18 IST 1970

Any one please help me out, how to convert current date as integer(10 digit number)

解决方案

The issue is that an Integer is not large enough to store a current date, you need to use a Long.

The date is stored internally as the number of milliseconds since 1/1/1970.

The maximum Integer value is 2147483648, whereas the number of milliseconds since 1970 is currently in the order of 1345618537869

Putting the maximum integer value into a date yields Monday 26th January 1970.

Edit: Code to display division by 1000 as per comment below:

    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

这篇关于将当前日期转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆