18

i have a String x = "1086073200000" . This is basically millisecond which I need to convert to a Date.

To convert i am using

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

The problem is when i convert the string x to long , some digits go away due to the limit on the size of long.

How do I preserve the entire String.

THanks.

jseth
  • 1,833
  • 3
  • 13
  • 8
  • 1
    So your code doesn't relate to the question at all? - you don't show the string to long conversion you claim is causing problems... – John3136 Sep 20 '12 at 00:13
  • @John3136: He's converting the long (milliseconds) into a date and trying to print it "long -> string". – Borgleader Sep 20 '12 at 00:16
  • @John3136,&Borleader I am reading a file,so input is String , which i need to convert to long so that I can convert it to date. – jseth Sep 20 '12 at 00:18
  • 2
    So why are you using `Double.parseDouble()`? – Jon Lin Sep 20 '12 at 00:18
  • 1
    Can you post the file reading part? Your code is perfectly fine, the problem must be in the `String` that you are reading. – João Silva Sep 20 '12 at 00:20

5 Answers5

42
double tempo=Double.parseDouble(z);

Why are you parsing your String which is supposed to be a Long as a Double?

Try using 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())); 
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Tim Bender
  • 20,112
  • 2
  • 49
  • 58
9

I tried this code and it worked for me

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)); 
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
enTropy
  • 621
  • 4
  • 14
5

tl;dr

Instant.ofEpochMilli ( 1_346_482_800_000L );

2012-09-01T07:00:00Z

Details

The accepted answer by Tim Bender and other answer by enTropy are both correct. You were parsing your input string as a double rather than as a long.

java.time

Also, the Question and Answers are using old outmoded date-time classes. These are now supplanted by the java.time package. See Oracle Tutorial. Much of the java.time functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

Count from epoch

The java.time classes count from the same epoch as the old classes, first moment of 1970 in UTC. So we can start the same way, with parsing the input as a long.

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

UTC

Use that long number to get an Instant object. This class represents a moment on the time line in UTC, similar in concept to the old java.util.Date. But the new classes have a resolution up to nanoseconds whereas the old are limited to milliseconds. Fortunately the Instant class has a convenient factory method taking a count of milliseconds-since-epoch, ofEpochMilli.

Instant instant = Instant.ofEpochMilli ( millis );

Time zone

Now assign a time zone (ZoneId) to get a ZonedDateTime object. This is similar in concept to a java.util.Calendar in that it represents a moment on the timeline with a specific assigned time zone.

Always specify the desired/expected time zone. Though optional, never omit the time zone as you did in your use of Calendar in the Question. If omitted, the JVM’s current default time zone is silently applied. This default can vary from machine to machine, and from time to time, even during runtime(!). Better to specify than assume.

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

Dump to console. Notice how the hour-of-day is 07 in UTC but 03 in Québec time as that zone had an offset of -04:00, four hours behind UTC, during the summer under Daylight Saving Time (DST).

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

input: 1086073200000 | millis: 1086073200000 | instant: 2004-06-01T07:00:00Z | zdt: 2004-06-01T03:00-04:00[America/Montreal]

A String is not a date-time

How do I preserve the entire String.

Do not confuse a String of text describing the value of a date-time with the date-time itself. A date-time object can generate a String to represent its value, but that String is separate and distinct from the date-time object.

To generate a String from a java.time object such as ZonedDateTime, either:

  • Call toString to get a String in standard ISO 8601 format.
    (as seen in example code above)
  • Use DateTimeFormatter to:
    (a) Define your own custom format, or
    (b) Automatically localize to the user’s human language and cultural norms.

Search Stack Overflow for many other Questions and Answers on generating formatted Strings from DateTimeFormatter.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
4
Date date = new Date(milliseconds);
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jaya
  • 999
  • 11
  • 8
0

Just do this ::::

import java.util.Date;

class HelloWorld {
    public static void main(String[] args)
    {
        String str = "1346482800000";
        Long tmStmp = Long.parseLong(str);
        Date date = new Date(tmStmp);
        System.out.println(date);
    }
}
Gaur 97
  • 73
  • 5
  • 1
    One, just avoid the `Date` class altogether, it’s poorly designed and long outdated. Use `java.time.Instant` as shown in the answer by Basil Bourque. Two, what you say is already in the two topmost answers. – Ole V.V. Sep 08 '22 at 14:49