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?
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.
I am not sure if "Israel" is a valid timezone, try "Asia/Jerusalem", have a look at this post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With