tl;dr
ZonedDateTime.now(
ZoneId.of( "Europe/Paris" )
)
.withZoneSameInstant(
ZoneId.of( "Pacific/Auckland" )
)
.format(
DateTimeFormatter.ofLocalizedDateTime(
FormatStyle.FULL
)
.withLocale( Locale.ITALIAN )
)
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 );
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() ;
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?