Java - local time

I am trying 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 always leads me 7 hours less from the current time in Israel, who has an idea how to achieve the current time in Israel?

+4
source share
3 answers

You set the time zone in your calendar, but you must set it in your own DateFormat. In addition, you should use Asia/Jerusalemas the name of the time zone. You do not need it at all Calendar- it only new Date()gives the current moment:

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

, ( ), , , JVM, , .

+11

, "" , "/", this post

+3

Locale TimeZone, .

Locale aLocale = new Locale.Builder().setLanguage("iw").setRegion("IL").build();
DateFormat timeFormat = new SimpleDateFormat("MMM y HH:mm", aLocale);
timeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Jerusalem"));
String curTime = timeFormat.format(new Date());

.. , , , OP .

+2

Source: https://habr.com/ru/post/1540064/


All Articles