-5

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
zaozaoer
  • 361
  • 1
  • 3
  • 14
  • is this what you are looking for https://stackoverflow.com/questions/59329786/how-to-create-a-long-time-in-milliseconds-from-string-in-java-8-with-localdateti/59330041#59330041 ? – Ryuzaki L Dec 30 '19 at 21:25
  • @Deadpool, thanks for your quick reply, I am looking for the solution the other way around: convert a long value to Date Object. (And I should add that I am on java 7). – zaozaoer Dec 30 '19 at 21:29
  • show the code that you have tried ? – Ryuzaki L Dec 30 '19 at 21:29
  • 1
    Have you tried using the constructor public Date(long date) described at https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#Date(long)? (and then use SimpleDateFormat, which is in java 7 https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#Date(long)) – Jeremy Kahan Dec 30 '19 at 21:39
  • I recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `Instant`, `ZoneId` and `ZonedDateTime`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Yes, they work on Java 7. Add [ThreeTen Backport](https://www.threeten.org/threetenbp/) to your project and you’re flying. :-) – Ole V.V. Jan 02 '20 at 18:53
  • Does this answer your question? [Java convert millisecond timestamp to date with respect to given timezone](https://stackoverflow.com/questions/48714322/java-convert-millisecond-timestamp-to-date-with-respect-to-given-timezone) – Ole V.V. Jan 02 '20 at 18:53
  • 1
    Thanks everybody who commented or edited this post. Sorry, i was in a hurry, my question was likely ambiguous. I will remember to ask questions clearer, hopefully with sample code. – zaozaoer Jan 02 '20 at 20:05

3 Answers3

2

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]

Jeremy Kahan
  • 3,796
  • 1
  • 10
  • 23
1

tl;dr

new Date().setTime( milliseconds )

java.util.Date::setTime

I 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.Instant

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

Strings

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.


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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks for your comments and editing, unfortunately, I cannot pick multiple best answers, yours was very helpful. – zaozaoer Jan 02 '20 at 20:07
0

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");
CodingSamples
  • 194
  • 1
  • 1
  • 7