Java如何将UTC毫秒转换为UTC日期

Java如何将UTC毫秒转换为UTC日期,java,utc,Java,Utc,我有一个int 1446159600,它是UTC/GMT日期,即2015年10月29日星期四23:00:00 GMT。我尝试将其转换为实际UTC日期,但无法使用日历、SimpleDateFormat和时区类。有人能帮我吗?实际上1446159600毫秒是“1970年1月17日星期六23:12:39”。这是我的当地时间 但是,您可以使用如下日历对象获取关联的日期 Calendar cal = new GregorianCalendar(); cal.setTimeInMillis(

我有一个int 1446159600,它是UTC/GMT日期,即2015年10月29日星期四23:00:00 GMT。我尝试将其转换为实际UTC日期,但无法使用日历、SimpleDateFormat和时区类。有人能帮我吗?

实际上1446159600毫秒是“1970年1月17日星期六23:12:39”。这是我的当地时间

但是,您可以使用如下日历对象获取关联的日期

    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(1446159600 );
    System.out.println(cal.getTime());
这里的输出正好是“Sat Jan 17 23:12:39 IST 1970”

此外,如果要将毫秒转换为日期,可以使用
它给出了与给定毫秒相关的日期。

也许我误解了这个问题,但这会从当前时间创建一个以毫秒为单位的日期:

public class DateTime
{
  public static void main (String[] args)
  {
    long time = System.currentTimeMillis ();
    System.out.println (time);
    Date date = new Date (time);
    System.out.println (date);
  }
}
哪些产出:

1446191239738
Fri Oct 30 18:47:19 AEDT 2015

时间戳似乎以秒为单位,而大多数java使用毫秒,因此我们必须乘以1000

    long timestampMilliseconds = 1446159600*1000L;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    String stringDate = simpleDateFormat.format(new Date(timestampMilliseconds));
    System.out.println(stringDate); // Thu, 29 Oct 2015 23:00:00 GMT
1000后的L是这样的,乘法作为长值进行,并且数字不会溢出整数max值。您可以使用
((long)1446159600)*1000
1446159600L*1000
或其他任何工具来获得相同的效果。您也可以使用
TimeUnit.SECONDS.toMillis(1446159600)
;博士 2015-10-29T23:00:00Z

纪元 您似乎有一个整数计数-以1970年的第一个时刻为纪元计算整秒

您引用的旧日期时间类基于毫秒。所以乘以1000

java.time 更好的是,完全避免那些旧的课程。Java8及更高版本中的新框架是一个巨大的改进,取代了那些旧类。看见这些新类的分辨率为,或在几分之一秒内保留9位小数

这个类(UTC时间轴上的一个时刻)甚至有一个方便的方法,所以不需要乘以1000

long wholeSecondsFromEpochInUtc = 1_446_159_600L;
Instant instant = Instant.ofEpochSecond ( wholeSecondsFromEpochInUtc );
这一行就解决了你的问题

格式化 我们可以走得更远。将时区应用于该瞬间以获得一个时间间隔

转储到控制台

System.out.println ( "instant (UTC): " + instant );
System.out.println ( "zdt: " + zdt );
System.out.println ( "output: " + output );
即时(UTC):2015-10-29T23:00:00Z

zdt:2015-10-29T19:00-04:00[美国/蒙特利尔]

产出:朱迪2015年10月29日美国东部夏令时19时00分


检查此代码:

    // Make a format for showing the date
    DateFormat dateFormat = new SimpleDateFormat("dd MMM, yyy", Locale.FRANCE);
    // Make a format for showing the time
    DateFormat timeFormat = new SimpleDateFormat("hh:mm a", Locale.FRANCE);

    // Get the current time in UTC (in milliseconds since 01 Jan 1970)
    Long currentUTCDate = Calendar.getInstance().getTimeInMillis();

    // Convert the current time from UTC to normal date
    Date currentDate = new Date(currentUTCDate);

    // Take the date only from currentDate object using the dateFormat we made before
    // and pass it to currentDateOnly String
    String currentDateOnly = dateFormat.format(currentDate);
    // Do the same with the time
    String currentTimeOnly = timeFormat.format(currentDate);

在这段代码中,我们可以区分三个主要的不同类:
1.
SimpleDataFormat()
,它返回的对象类型为
DateFormat

2.
Calendar
,其中一种方法是
getTimeInMillis()

3.
Date
,该对象可以保存有关您提供时间的所有细节

现在:
.
getTimeInMillis()
返回一个Long类型的数字,它保存自1970年1月1日以来的当前毫秒数。
我们使用这种类型的时间来简化日期计算。
. 在
Date
类中,我们有一个构造函数,它将
getTimeInMillis()
中的长数作为参数,并为我们返回一个
Date
类型的对象,该对象保存了我们在构造函数中给出的时间的每个细节。
. 现在我们不需要对象
currentDate
包含的所有细节,因此我们制作了一个过滤器,只从
currentDate
对象中获取日期和时间,并使用
DateFormat
类型的对象来实现这一点 . 制作一个类型为
DateFormat
的对象,我们使用
SimpeDateFormat()
构造函数,并将一个
参数作为一个
字符串传递给它,该字符串是一个特定的
模式
,我们希望从
当前日期
对象中获取,例如(“dd MMM,yyy”),它在我们的代码中:

DateFormat dateFormat = new SimpleDateFormat("dd MMM, yyy", Locale.FRANCE);
。最后,我们将
currentDate
对象作为
paremeter
添加到
dateFormat.format()
方法中,该方法将返回一个与我们给出的模式相同的过滤日期字符串,在我们的示例中是
“dd MMM,yyyy”
,结果类似于:

2018年9月8日

什么Java版本?7.8?时间戳似乎以秒为单位,java以毫秒为单位。记住这一点。类似的:这很有趣,如果我去,输入这个数字,我会得到2015年10月29日。这是否意味着unix历元时间与UTC时间的毫秒数不同?看起来“epochconverter”需要秒而不是毫秒。这就是问题所在:由于某种原因,返回的时间是在PDT中的。您是否居住在该时区?对不起,我试图避免使用本地时区,UTC时间仅作为日期而不是字符串。但我想确认它位于UTC时区中。谢谢。@user270811但它只是一个长整数,您已经说过它是UTC日期。这可能会有帮助-很抱歉我正在使用java 7。@user270811查看java 6和7的Three Ten额外项目。请进一步解释。这些可怕的类在几年前被现代的java.time类取代。建议在2018年使用它们是一个糟糕的建议。嗯,我是新来的,我找不到更好的东西。很抱歉,我提供了一些无用的信息。谢谢你路过
System.out.println ( "instant (UTC): " + instant );
System.out.println ( "zdt: " + zdt );
System.out.println ( "output: " + output );
    // Make a format for showing the date
    DateFormat dateFormat = new SimpleDateFormat("dd MMM, yyy", Locale.FRANCE);
    // Make a format for showing the time
    DateFormat timeFormat = new SimpleDateFormat("hh:mm a", Locale.FRANCE);

    // Get the current time in UTC (in milliseconds since 01 Jan 1970)
    Long currentUTCDate = Calendar.getInstance().getTimeInMillis();

    // Convert the current time from UTC to normal date
    Date currentDate = new Date(currentUTCDate);

    // Take the date only from currentDate object using the dateFormat we made before
    // and pass it to currentDateOnly String
    String currentDateOnly = dateFormat.format(currentDate);
    // Do the same with the time
    String currentTimeOnly = timeFormat.format(currentDate);
DateFormat dateFormat = new SimpleDateFormat("dd MMM, yyy", Locale.FRANCE);