Issue
My requirement is to get the current date and time of on the basis of country code like (DE, IT ,ES, IE , PT, UK). Is that any Java API available for this?
I tried setting locale of specified country as well but I am getting same output?
Can some one tell me why I am getting "Asia/Kolkata" time zone even I am setting the locale as well?
public void timezone() {
final Locale spanishLocale = new Locale("es", "ES");
final Locale ukLocale = new Locale("en", "UK");
final Locale deLocale = new Locale("de", "DE");
final Locale itLocale = new Locale("it", "IT");
final Locale ptLocale = new Locale("pt", "PT");
final Locale ieLocale = new Locale("es", "ES");
final Calendar cal = Calendar.getInstance(spanishLocale);
System.out.println("date and time in spain is " + cal.getTime());
System.out.println(
"date and time in uk is " + Calendar.getInstance(ukLocale).getTime() + " :: " + Calendar.getInstance(ukLocale).getTimeZone());
System.out.println(
"date and time in de is " + Calendar.getInstance(deLocale).getTime() + " :: " + Calendar.getInstance(deLocale).getTimeZone());
System.out.println(
"date and time in it is " + Calendar.getInstance(itLocale).getTime() + " :: " + Calendar.getInstance(itLocale).getTimeZone());
System.out.println(
"date and time in pt is " + Calendar.getInstance(ptLocale).getTime() + " :: " + Calendar.getInstance(ptLocale).getTimeZone());
System.out.println(
"date and time in ie is " + Calendar.getInstance(ieLocale).getTime() + " :: " + Calendar.getInstance(ieLocale).getTimeZone());
}
out put :
date and time in spain is Sat Apr 07 20:23:52 IST 2018
date and time in uk is Sat Apr 07 20:23:52 IST 2018 :: sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
date and time in de is Sat Apr 07 20:23:52 IST 2018 :: sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
date and time in it is Sat Apr 07 20:23:52 IST 2018 :: sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
date and time in pt is Sat Apr 07 20:23:52 IST 2018 :: sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
date and time in ie is Sat Apr 07 20:23:52 IST 2018 :: sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
Solution
tl;dr
ZonedDateTime.now( // Capture the current moment as seen by the wall-clock time used by the people of a certain region (a time zone).
ZoneId.of( "Europe/Paris" ) // Specify time zone by official IANA time zone name. https://en.wikipedia.org/wiki/Tz_database
) // Returns a `ZonedDateTime` object.
.withZoneSameInstant( // Adjust from Paris time to Auckland time, just to show that we can. Same moment, different wall-clock time.
ZoneId.of( "Pacific/Auckland" )
) // Returns a new, second `ZonedDateTime` object without changing (“mutating”) the first. Per immutable objects pattern.
.format( // Generate a String representing the value of our `ZonedDateTime`.
DateTimeFormatter.ofLocalizedDateTime( // Let java.time automatically localize.
FormatStyle.FULL // Specify the length/abbreviation of new String.
) // Returns a `DateTimeFormatter` using the JVM’s current default `Locale`. Override this default in next line.
.withLocale( Locale.ITALIAN ) // Locale is unrelated to time zone. Wall-clock time of Auckland, presented in Italian – perfectly reasonable depending on the needs of your user.
) // Returns a String object holding text that represents the value of our `ZonedDateTime` object.
domenica 8 aprile 2018 08:48:16 Ora standard della Nuova Zelanda
Country does not determine time zone
Current date and time on the basis of country code
No can do.
There is no direct link between country and time zone.
Geographically large countries often straddle multiple time zones, so that the wall-clock time of each region keeps close to solar time (meaning “noon” is when the sun is overhead). Present-day India is unusual in using one time zone across its vast land mass.
Also, there are often enclaves within a country that use a different time zone. This is often related to joining or refusing Daylight Saving Time (DST). For example, in the United States the state of Arizona opts out of the silliness of DST. Yet within Arizona is part of the Navajo Nation which does participate in DST. So neither country nor state/province determines time zone.
Furthermore, there is more to think about than the current offset-from-UTC. A time zone is a history of past, present, and future changes to the offset used by the people of a region. Cutovers in Daylight Saving Time (DST) is one common cause of the offset changing in a zone. Determining earlier/later moments requires a time zone to apply these historical adjustments.
So forget about countries. Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter pseudo-zones such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
Country code as a hint
You could guess, or present a short list for the user to choose from, based on their current geolocation or country code. See the Answer by well on this page for a data file listing the approximately 350 time zones in use since 1970 along with their longitude/latitude and country code.
Excerpt:
…
TK -0922-17114 Pacific/Fakaofo
TL -0833+12535 Asia/Dili
TM +3757+05823 Asia/Ashgabat
TN +3648+01011 Africa/Tunis
TO -2110-17510 Pacific/Tongatapu
TR +4101+02858 Europe/Istanbul
…
Locale & time zone are orthogonal issues
i tried setting locale of specified country as well but i am getting same output ?
Locale has nothing to do with time zone & offset-from-UTC. Locale determines the human language and cultural norms used in localization when generating strings to represent the date-time value. So locale does not affect the meaning.
To localize, specify:
FormatStyle
to determine how long or abbreviated should the string be.Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
For example, consider an engineer from Québec attending a conference in India. She will want to view the conference schedule using the Asia/Kolkata
time zone to match the time-of-day seen on the clocks on the walls.
LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;
LocalTime lt =LocalTime.of( 14 , 0 ) ;
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ; // Conference session start.
2018-01-23T14:00+05:30[Asia/Kolkata]
But our engineer would prefer to read the text and formatting in her native French, Locale.CANADA_FRENCH
.
Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = zdt.format( f );
mardi 23 janvier 2018 à 14:00:00 India Standard Time
Other time zones
My requirement is to get the current date and time of on the basis of country code like (DE, IT ,ES, IE , PT ,UK)
You can make a new ZonedDateTime
using a different time zone but with the same moment (Instant
) inside. So you will see a different wall-clock time, yet continue to refer to the same moment, same point on the timeline.
ZoneId zMadrid = ZoneId.of( "Europe/Madrid" ) ;
ZonedDateTime zdtMadrid = zdt.withZoneSameInstant( zMadrid) ; // Same moment, different wall-clock time.
zdtMadrid.toString(): 2018-01-23T09:30+01:00[Europe/Madrid]
Perhaps localize that Spain-zoned moment in Japanese.
DateTimeFormatter fJapan = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.JAPAN );
String outputJapan = zdtMadrid.format( fJapan ); // Zone & locale are unrelated, orthogonal issues.
System.out.println(outputJapan);
So we get a Madrid time presented in Japanese language/culture.
2018年1月23日火曜日 9時30分00秒 中央ヨーロッパ標準時
UTC
Generally speaking, you should use UTC values for much of you business logic, logging, storage, and exchange. Simply extract a Instant
from our ZonedDateTime
. Both the Instant
and ZonedDateTime
represent the same moment, the same point on the timeline, but viewed using a different wall-clock time.
Instant instant = zdt.toInstant() ; // Capture current moment in UTC.
Going the other direction:
ZonedDateTime zdt = instant.atZone( "Europe/Helsinki" ) ; // Same simultaneous moment, different wall-clock time.
Avoid legacy classes
The troublesome old date-time classes were supplanted years ago by the java.time classes. Instead of Calendar
use ZonedDateTime
.
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
For a moment in UTC, Use Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = Instant.now() ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 brought some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android (26+) bundle implementations of the java.time classes.
- For earlier Android (<26), the process of API desugaring brings a subset of the java.time functionality not originally built into Android.
- If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
Answered By - Basil Bourque
Answer Checked By - Willingham (JavaFixing Volunteer)