[英]Obtain Date object from the int value in java
I have a int
value and from int
value I want to obtain Date
object for that int
value. 我有一个int
值,并且要从int
值中获取该int
值的Date
对象。
Example: 例:
int value = 166368;
long l = (long)value;
Date date = new Date(l);
Value is my integer value for which I want Date
object for this particular value. 值是我想要此特定值的Date
对象的整数值。
Now I want to obtain Date
for this int
value. 现在,我想获取此int
值的Date
。 I try to convert this int
value to long
and then set the long
value in Date
object but it not return correct value. 我尝试将此int
值转换为long
,然后在Date
对象中设置long
值,但不会返回正确的值。
So how to achieve this? 那么如何实现呢?
The value that you pass to the Date constructor needs to be the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT. 您传递给Date构造函数的值必须是自格林尼治标准时间1970年1月1日00:00:00.000以来经过的毫秒数。
Your code is correct, but the value that you are passing in here seems much too small to be correct. 您的代码是正确的,但是您在此处传递的值似乎太小而无法正确。
If the long value you pass is milliseconds since the epoch (1-Jan-1970), and 166368 ms = 166.3 seconds < 3 min, I'd say that you should be getting 1-Jan-1970 as the Date value. 如果您传递的长值是距纪元(1970年1月1日)的毫秒数,并且166368 ms = 166.3秒<3分钟,那么我想说您应该将1970年1月1日作为Date值。 Correct? 正确?
as @ggreiner pointed out, you should tell us, which date are you expecting to get from int value 166368? 正如@ggreiner所指出的,您应该告诉我们,您希望从int值166368获得哪个日期?
your question "Obtain Date object from the int value in java" is possible: 您的问题“从java中的int值获取Date对象”是可能的:
final int x = Integer.MAX_VALUE;
final Date d = new Date(x);
final Date today = new Date();
System.out.println("max integer x:" + x);
System.out.println("the Max date could be represented by x:" + d);
System.out.println("today in number:" + today.getTime());
run the above code, you got: 运行上面的代码,您得到:
max integer x:2147483647
the Max date could be represented by x:Sun Jan 25 21:31:23 CET 1970
today in number:1330362276028
which means, if you pass integer to Date() constructor, it works, but you won't get Date later than Sun Jan 25 21:31:23 CET 1970
. 这意味着,如果将整数传递给Date()构造函数,则可以使用,但不会晚于Sun Jan 25 21:31:23 CET 1970
获得Date。
If you check the API public Date(long date)
does this 如果您检查API public Date(long date)
是否这样做
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT. 分配一个Date对象,并将其初始化为表示自标准基准时间(即“纪元”)(即GMT 1970年1月1日)以来指定的毫秒数。
166368 is hardly 3 minutes, So the date is not going to change. 166368几乎不是3分钟,因此日期不会改变。 If you print you will find a difference only in time. 如果您进行打印,只会发现时间有所不同。
May be you are trying to do something like this 可能是您正在尝试做这样的事情
Date now = new Date();
long nowLong = now.getTime();
long newLong = nowLong + 166368;
System.out.println(new Date(newLong).toString());
Neat way to play with dates would be to use DateFormat class. 使用日期的最佳方法是使用DateFormat类。
DateFormat df = new SimpleDateFormat("dd/MM/yy"); DateFormat df = new SimpleDateFormat(“ dd / MM / yy”); String formattedDate = df.format(new Date()); 字符串formattedDate = df.format(new Date());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.