android - List of supported Locale Times
itemprop="text">
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;
class="post-text" itemprop="text">
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
No comments:
Post a Comment