0

I need to get Locale Time of Nepal but could not get through it. How to get Locale Time for Nepal, which is GMT+5:45? How can I get through it? How to change the Locale, English according to our local time? Here date is in English Standard.

Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ENGLISH).parse(date);
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    String timeName = new SimpleDateFormat("hh:mm a").format(cal.getTime());
    return timeName;
  • Do you want to get the **local time** of Nepal ? If that's the case, [take a look at this](https://stackoverflow.com/questions/16202956/get-current-time-in-a-given-timezone-android) – Arthur Attout Oct 05 '18 at 07:32
  • Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. `ZonedDateTime.now(ZoneId.of("Asia/Kathmandu"))`.If your API level is 26 or higher, `ZonedDateTime` and `ZoneId` are built in. If lower you can get them in the ThreeTenABP library. The two classes are part of java.time. – Ole V.V. Oct 05 '18 at 10:59
  • In which time zone is `date`? Please edit your question to make it clear what you want to convert from. Also please search before asking, I am sure you can find relevant questions and answers to help you. English usage: I think you mean *local* time (not *locale* time, I don’t know what that would mean). – Ole V.V. Oct 05 '18 at 11:03
  • date is in English timezone. @OleV.V. – helpline technologies Oct 08 '18 at 06:34
  • Thanks for making this precise, @helplinetechnologies. I have updated my answer to reflect it. – Ole V.V. Oct 08 '18 at 06:44

1 Answers1

1

The time zone ID to use for Nepal time is Asia/Kathmandu. Time zone IDs generally have the format region/city, where city is the largest populated area in the time zone (not necessarily a capital; for example the time used in Beijing is Asia/Shanghai, and in Delhi it is Asia/Kolkata).

You get the set of supported time zone IDs from ZoneId.getAvailableZoneIds(). And "Asia/Kathmandu" is a member of the returned set.

Locale and time zone are different and unrelated concepts (even though each is often associated with a geographical area, but not always). Locale has to do with language and culture, not with time.

So to convert for example a time of 2018-10-08 20:42:53 in the United Kingdom (England, Northern Ireland, Wales and Scotland) to Nepal time:

    DateTimeFormatter fromFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    ZoneId fromZone = ZoneId.of("Europe/London");
    DateTimeFormatter toTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
            .withLocale(Locale.US);
    ZoneId toZone = ZoneId.of("Asia/Kathmandu");

    String englishDateTime = "2018-10-08 20:42:53";
    ZonedDateTime dateTimeInEngland = LocalDateTime
            .parse(englishDateTime, fromFormatter)
            .atZone(fromZone);
    LocalTime timeInNepal = dateTimeInEngland.withZoneSameInstant(toZone)
            .toLocalTime();

    System.out.println(timeInNepal.format(toTimeFormatter));

Output is:

1:27 AM

I am using java.time, the modern Java date and time API. I much prefer it over the outdated date and time classes Date, SimpleDateFormat and Calendar that you used in the question.

Question: Can I use java.time on Android?

Yes, java.time works nicely on Android devices. It just requires at least Java 6.

  • In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I got error -> System.err: org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered – helpline technologies Oct 08 '18 at 07:46
  • It looks like you have hit the error described [in this question: ThreeTen-Backport error on Android - ZoneRulesException: No time-zone data files registered](https://stackoverflow.com/questions/38281808/threeten-backport-error-on-android-zonerulesexception-no-time-zone-data-files). I am sorry, that is not my home field. Your package name, `org.threeten.bp.zone`, confirms that you have got the right library. I have run my code on ThreeTen Backport, but I don’t have an Android development environment to test it on. If you can’t solve it, I think it’s best to post a new question – Ole V.V. Oct 08 '18 at 07:54
  • 1
    I solved at by adding **AndroidThreeTen.init(this)** in **onCreate()**. – helpline technologies Oct 08 '18 at 10:45