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
Post a Comment