How to convert a long value to Date object in the format of "yyyy-MM-dd HH:mm:ss" (in timezone EST). I am looking for the solution the other way around: convert a long value to Date Object.
Using Java 7.
How to convert a long value to Date object in the format of "yyyy-MM-dd HH:mm:ss" (in timezone EST). I am looking for the solution the other way around: convert a long value to Date Object.
Using Java 7.
The following approach should work even in java 7.
Date d = new Date(1527152472);//pass in whatever long value you have to the date constructor
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//sets the format you want
TimeZone etTimeZone = TimeZone.getTimeZone("America/New_York");
simpleDateFormat.setTimeZone(etTimeZone);// sets the time zone of the formatter
System.out.println(simpleDateFormat.format(d));//gives the formatted date
references: https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#Date(long) [shows date constructor with long argument in java 7 -- not deprecated]
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html [shows SimpleDateFormat in java 7]
https://howtodoinjava.com/java/date-time/convert-date-time-to-est-est5edt/ [I used the approach for Java version < 8. See there for an extended discussion about America/New_York versus getTimeZone("EST") which might be more what you want -- but according to the author there, probably not]
new Date().setTime( milliseconds )
java.util.Date::setTimeI am looking for the solution the other way around: convert a long value to Date Object
Assuming by "a long value" you mean a count of milliseconds since the first moment of 1970 in UTC:
java.util.Date d = new Date() ;
d.setTime( millis ) ;
org.threeten.bp.InstantThe java.util.Date class is terrible, and is now legacy. Supplanted years ago by the modern java.time classes. Specifically replaced by the java.time.Instant class.
The java.time classes are built into Java 8 and later. For Java 6 & Java 7, use the back-port library, ThreeTen-Backport.
org.threeten.bp.Instant instant = Instant.ofEpochMilli( millis ) ;
Convert to a java.util.Date if you must interoperate with old code.
java.util.Date d = org.threeten.bp.DateTimeUtils.toDate( instant ) ;
Vice versa.
org.threeten.bp.Instant instant = org.threeten.bp.DateTimeUtils.toInstant( d ) ;
As for…
Date object in the format of "yyyy-MM-dd HH:mm:ss" (in timezone EST)
…date-time objects do not have a “format”. They can produce text representing their internal value in various formats. But the strings they produce, or parse, are distinct and separate from the date-time objects.
As for "timezone EST", there is no such thing. Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).
I will assume that by "EST" you meant some time zone such as America/Montreal or America/New_York.
org.threeten.bp.ZoneId z = ZoneId.of( "America/New_York" ) ;
Apply that zone to our Instant to produce a ZonedDateTime object. Same moment, different wall-clock time.
org.threeten.bp.ZonedDateTime zdt = instant.atZone( z ) ;
Generating a string representing part of the internal value of that ZonedDateTime object. Your desired format shows the date and the time but omits any mention of the zone or offset. I do not recommend omitting the zone/offset as the reader may misunderstand and assume the wrong zone.
We can use the predefined formatter ISO_OFFSET_DATE_TIME to get us close to your desired format. This formatter produces text in standard ISO 8601 format. The standard specifies a T between the date and time portions. We can replace that T with a SPACE to get your desired format.
org.threeten.bp.format.DateTimeFormatter f = DateTimeFormatter.ISO_OFFSET_DATE_TIME ;
String output =
zdt
.format( f )
.replace( "T" , " " )
;
If you want to hide the fractional second, you could truncate. The java.time classes use immutable objects, so a fresh ZonedDateTime object will be instantiated. Your original ZonedDateTime object will not be altered.
org.threeten.bp.format.DateTimeFormatter f = DateTimeFormatter.ISO_OFFSET_DATE_TIME ;
String output =
zdt
.truncatedTo( org.threeten.bp.temporal.ChronoUnit.SECONDS ) // Drops the fractional second, producing a new fresh `ZonedDateTime` object.
.format( f )
.replace( "T" , " " )
;
Instead of this manipulations, you could define a custom formatting pattern. Search Stack Overflow as this has been covered many times already.
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?
How do you get the long value? If you get the thime with System.currentTimeMillis it should be im UTC. You can create a new Date object with the long value and use this solution to convert it to EST
To format the date you can use SimpleDateFormat
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");