Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Try with Locale along with TimeZone if you want to display date also along with time in your language.

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());

Note: This is not the answer of this question but just here if in case OP is looking further for parsing date in Israel language.

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

import java.time.LocalTime;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now(ZoneId.of("Asia/Jerusalem"));
        System.out.println(time);
    }
}

Output from a sample run:

15:06:50.207521

ONLINE DEMO

If you want the format to be restricted to HH:mm, a couple of ways to to do so are:

  1. Truncating the LocalTime to ChronoUnit.MINUTES.
  2. Using DateTimeFormatter with the pattern, HH:mm.

Demo:

import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalTime timeHourMinute = LocalTime.now(ZoneId.of("Asia/Jerusalem"))
                                            .truncatedTo(ChronoUnit.MINUTES);
        System.out.println(timeHourMinute);

        // Alternatively
        LocalTime time = LocalTime.now(ZoneId.of("Asia/Jerusalem"));
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH);
        String strTime = dtf.format(time);
        System.out.println(strTime);
    }
}

Output from a sample run:

16:01
16:01

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top