Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java get current time in Israel

Tags:

java

timezone

I am tring to get the current time in Israel in java this is my code:

TimeZone timeZone = TimeZone.getTimeZone("Israel");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(timeZone);

DateFormat timeFormat = new SimpleDateFormat("HH:mm");
String curTime=timeFormat.format(calendar.getTime());

but it is always bring me 7 hours less from the current time in Israel someone have idea how to achieve the current time in Israel?

like image 521
ilay zeidman Avatar asked May 10 '14 19:05

ilay zeidman


2 Answers

You're setting the time zone in your calendar, but you should be setting it in your DateFormat. Additionally, you should be using Asia/Jerusalem as the time zone name. You don't need a Calendar at all - just new Date() gives the current instant:

DateFormat timeFormat = new SimpleDateFormat("HH:mm");
timeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Jerusalem"));
String curTime = timeFormat.format(new Date());

You should note that the time zone in Jerusalem is particularly hard to predict (it fluctuates a lot) so if the time zone data source that your JVM uses is out of date, it may be inaccurate.

like image 126
Jon Skeet Avatar answered Oct 08 '22 06:10

Jon Skeet


I am not sure if "Israel" is a valid timezone, try "Asia/Jerusalem", have a look at this post

like image 34
Kostas Chalkias Avatar answered Oct 08 '22 07:10

Kostas Chalkias