java - How to specify a timezone for a joda DateTime object? -


i have function converts string joda time object , formats it.

public static string getformatteddatetime(string somedate) {     datetime newdate = datetime.parse(somedate, datetimeformat.forpattern("yy-mm-dd hh:mm:ss.sss")).minushours(7);     string formatteddate = newdate.tostring("dd-mmm-yy hh:mm a");      return formatteddate; } 

the function takes string , returns formatted datetime fine, i'm having troubles assigning timezone it. string i'm passing in represents utc time database need convert pst/pdt. can't figure out way 1) assign new datetime object timezone , 2) convert object's timezone pst/pdt. right handling .minushours. not ideal.

under no circumstances should use .minushours(7) wrong half year, , datetime object still think it's in utc.

use .withzone(datetimezone.forid("america/los_angeles"));

here list of time zones supported joda time corresponding ids

i recommend refactoring constant generated forid() call static final field somewhere appropriate in code, , using everywhere need conversion.


you aware, on safe side datetime objects immutable, means withzone call return brand new datetime instance, , not change zone of original object.


@anotherdave makes great point in comments. pst in question, pst in use half year (because of daylight savings). assumed meant current time in los angeles, seattle, etc. , not pst zone, cities use pdt in other half of year. however, if want pst 100% of time, can use forid("pst");


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -