java - Calender.getTime() not showing the date time of given timeZone -


i in timezone +05:30 hrs , want time zone of europe, trying this:-

    timezone tmz=timezone.gettimezone("europe/zurich");     calendar calender=new gregoriancalendar(tmz);     date date=calender.gettime();     string datestr=new simpledateformat("yyyy-mm-dd hh:mm:ss").format(date);     system.out.println(datestr+" cst"); 

but getting timezone's time instead

you need set time zone in simpledateformat. date value doesn't have time zone, initial code pointless - call new date().

note format string incorrect - you're using minutes instead of months, , you're using 12-hour clock isn't want.

i suspect code should be:

timezone tmz = timezone.gettimezone("europe/zurich"); simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss", locale.us); format.settimezone(tmz); string datestr = format.format(new date()); 

as aside, if possibly can, avoid using these classes - use joda time if you're stuck on java 7 or earlier, , java.time package if you're using java 8. they're far, far better date/time apis.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -