How to set Joda date and time from TimeZone String ID and Epoch

I am reading the Joda Time library. I am trying to figure out how to build a DateTime object given an era timestamp and timezone. I hope this allows me to find the day of the week, like day, etc. For that time, in this time zone. However, I'm not sure how to pass the DateTimeZone to the DateTime constructor.

import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.Instant; public class TimeZoneTest { public static void main (String[] args) { long epoch = System.currentTimeMillis()/1000; DateTimeZone tz = new DateTimeZone( "America/New_York" ); DateTime dt = new DateTime( epoch, tz ); System.out.println( dt ); } } 

I tried the hardcoded America / New_York code above, but got it from the compiler. What am I doing wrong?

 $ javac -cp "joda-time-2.2.jar:." TimeZoneTest.java TimeZoneTest.java:12: org.joda.time.DateTimeZone is abstract; cannot be instantiated DateTimeZone tz = new DateTimeZone( "America/New_York" ); ^ 1 error 
+6
source share
1 answer

To get the time zone from the identifier, you use DateTimeZone.forID :

 DateTimeZone zone = DateTimeZone.forID("America/New_York"); 

As an aside, I donโ€™t think that โ€œeraโ€ is a good name for your variable โ€” it really is โ€œseconds from the Unix eraโ€. Also, I donโ€™t understand why you divide by 1000 ... the corresponding constructor for DateTime takes time zone and milliseconds since the Unix era ... so you can pass the value returned from System.currentTimeMillis() directly.

+10
source

All Articles