I am in timeZone +05:30 hrs and I want the time zone of Europe, so I am 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 I am getting my timezone's time instead
You need to set the time zone in the SimpleDateFormat
. A Date
value doesn't have a time zone, so your initial code is fairly pointless - you could just call new Date()
.
Note that your format string is incorrect too - you're using minutes instead of months, and you're using a 12-hour clock which almost certainly isn't what you want.
I suspect your 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 an aside, if you possibly can, I would avoid using all these classes - use Joda Time if you're stuck on Java 7 or earlier, and the java.time
package if you're using Java 8. They're far, far better date/time APIs.
See more on this question at Stackoverflow