转换毫秒字符串在Java日期

问题描述 投票:12回答:4

我有一个串x =“1086073200000”。这基本上是毫秒,我需要转换为日期。

要转换我使用

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

long tempo1=Long.parseLong(x);
System.out.println(tempo1);  // output is 86073200000 instead of the whole thing
long milliSeconds=1346482800000L;

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime())); 

问题是,当我在串x转换为长,有的数字走开由于对长大小的限制。

如何保持整个字符串。

谢谢。

java string date
4个回答
32
投票
double tempo=Double.parseDouble(z);

为什么你解析你的String这应该是一个Long作为Double

尝试使用Long.parseLong

String x = "1086073200000"

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

long milliSeconds= Long.parseLong(x);
System.out.println(milliSeconds);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime())); 

7
投票

我想这个代码和它的工作对我来说

public static void main(String[] args) {
    String x = "1086073200000";
    long foo = Long.parseLong(x);
    System.out.println(x + "\n" + foo);

    Date date = new Date(foo);
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    System.out.println(formatter.format(date)); 
}

3
投票

tl;dr

Instant.ofEpochMilli ( 1_346_482_800_000L );

2012-09-01T07:00:00Z

Details

accepted answer by Tim Benderother answer by enTropy都是正确的。你解析你输入的字符串作为double而不是作为long

java.time

此外,问题和答案都使用旧的过时的日期时间类。现在,这些被java.time包取代。见Oracle Tutorial。多的java.time功能已被后移植到Java 6和7在ThreeTen-Backport并且还适于在ThreeTenABP到Android。

从时代计数

该java.time类来自同一epoch作为老班,在UTC的1970年第一时刻计数。因此,我们可以开始以同样的方式,与解析输入作为long

String input = "1086073200000";
long millis = Long.parseLong ( input );

世界标准时间

使用long数得到一个Instant对象。此类表示在UTC时间线,在概念上与老java.util.Date类似的一个时刻。但新课程有一个分辨率高达nanoseconds而老仅限于milliseconds。幸运的是Instant类有一个方便的工厂方法以毫秒为单位纪元以来,ofEpochMilli的计数。

Instant instant = Instant.ofEpochMilli ( millis );

时区

现在,将一个时区(ZoneId)得到一个ZonedDateTime对象。这是在概念上与java.util.Calendar相似,因为它代表了一个特定的分配时区时间线上的时刻。

始终指定所需/预期的时区。虽然可选的,因为你在你的问题中使用Calendar的那样从未忽略的时区。如果省略,JVM当前的默认时区默默应用。该默认可以改变从机器到机器,并且不时地,甚至在运行时(!)。最好指定比承担。

ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );

转储到控制台。注意小时的一天是如何07在UTC但03在魁北克时间下-04:00在暑假期间该区域已经偏移Daylight Saving Time (DST)的,背后UTC四个小时,。

System.out.println ( "input: " + input + " | millis: " + millis + " | instant: " + instant + " | zdt: " + zdt );

输入:1086073200000 |米利斯:1086073200000 |即时:2004-06-01T07:00:00Z | ZDT:2004-06-01T03:00-04:00 [美国/蒙特利尔]

字符串是不是日期时间

如何保持整个字符串。

不要混淆的文字描述的日期,时间与日期,时间本身的值的String。一个日期时间对象可以产生一个字符串来表示其值,但该字符串是从日期时间对象分离和不同。

为了产生从java.time对象的String如ZonedDateTime,或者:

  • 呼叫toString得到在标准ISO 8601格式的字符串。 (如见于例如代码以上)
  • 使用DateTimeFormatter到: (一)定义自己的自定义格式,或 (二)自动定位到用户的人类语言和文化规范。

搜索栈溢出了从DateTimeFormatter生成格式化的字符串许多其他问题和解答。


About java.time

java.time框架是建立在Java 8和更高版本。这些类取代的麻烦老legacy日期时间类,如java.util.DateCalendar,与SimpleDateFormat

该项目Joda-Time,现在在maintenance mode,建议迁移到java.time类。

要了解更多信息,请参见Oracle Tutorial。和搜索堆栈溢出了很多例子和解释。规格是JSR 310

从哪里获取java.time类?

该项目ThreeTen-Extra与java.time其他类延伸。该项目是为将来可能增加的java.time试验场。您可以在这里找到一些有用的类,如IntervalYearWeekYearQuartermore


3
投票
Date date = new Date(milliseconds);
© www.soinside.com 2019 - 2024. All rights reserved.