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
Instant.ofEpochMilli ( 1_346_482_800_000L );
2012-09-01T07:00:00Z
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
.
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.
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 );
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 );
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]
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:
Search Stack Overflow for many other Questions and Answers on generating formatted Strings from DateTimeFormatter
.
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.
Date date = new Date(milliseconds);
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));
}
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()));