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

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

问题描述

我想将当前日期转换为整数值.默认情况下,它返回long.当我尝试将long转换为整数,然后将整数值转换为date时,表示它显示了1970年的日期,

I want to convert the current date to integer value. By default, it returns long. When I try to convert long to integer, and afterwards 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));

输出如下:

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位数字)?

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

推荐答案

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

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

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

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

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

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

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

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

按以下注释显示除以1000的代码:

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天全站免登陆