tl;dr
Instant // Represent a moment in UTC with resolution of nanoseconds.
.ofEpochMilli( // Parsing a count of milliseconds.
1_567_697_400_000L // Count of whole seconds since first moment of 1970 in UTC.
) // Returns an `Instant` object.
.atZone( // Adjust from UTC to some time zone. Same moment, different wall-clock time.
ZoneId.of( "America/Los_Angeles" ) // Use proper time zone names in `Continent/Region` format rather than mere offset-from-UTC (hours-minutes-seconds).
) // Returns a `ZonedDateTime` object.
.toString() // Generate text in standard ISO 8601 format, wisely extended to append name of time zone in square brackets.
Details
The other two Answers by Aditi Gupta and by Ole V.V. are both correct. I'll add a bit of example code.
You are using terrible date-time classes year ago supplanted by java.time classes.
Instant
I am trying to convert a timestamp (milliseconds)
If your count of milliseconds is since the epoch reference of first moment of 1970 in UTC, parse as a Instant
.
long input = 1_567_697_400_000L ; // Count of milliseconds since 1970-01-01T00:00:00Z.
Instant instant = Instant.ofEpochMilli( input ) ;
instant.toString(): 2019-09-05T15:30:00Z
Time zone
to another time zone (GMT-7:00 America/Los Angeles)
Use a proper time zone name rather than a mere offset-from-UTC.
Apply a time zone (ZoneId
) to your Instant
to adjust into a time zone, yielding a ZonedDateTime
object.
ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
zdt.toString(): 2019-09-05T08:30-07:00[America/Los_Angeles]
Always specify time zone
My Local timezone is "GMT+5:30"
Your own local time zone should be irrelevant to your date-time handling, as should the JVM’s default time time.
In java.time, the time zone and offset arguments are optional. If omitted, the JVM’s current default time zone is implicitly applied. This is one of the very few flaws in the design of java.time, in my opinion – those zone arguments should be required. I recommend you always specify your desired/expected time zone explicitly.
We can adjust to your own time zone if you care to see. Ask for the JVM’s current default time zone explicitly to make the intentions of your code crystal-clear to the reader.
ZoneId zDefault = ZoneId.systemDefault() ;
ZonedDateTime zdtDefault = zdt.withZoneSameInstant( zDefault ) ;
zdtDefault.toString(): 2019-09-05T21:00+05:30[Asia/Kolkata]
IdeOne.com demo
See all this code run live at IdeOne.com.
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
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.