tl;dr
ZonedDateTime.now( ZoneId.of( "India/Kolkata" ) )
Servers in UTC
Your servers should generally be set to UTC time. But you should never depend on that. Always specify the desired/expected time zone explicitly.
String != Date-Time
As others explain, do not conflate a date-time value with its String representation. A date-time object can parse Strings, and can generate Strings, but is distinct and separate from those Strings. A date-time object is not a String.
Avoid old date-time classes
You are using troublesome old legacy date-time classes now supplanted by java.time classes.
Use java.time
From the comments it sounds like you are merely asking for the current moment to be captured in the India time zone. India time nowadays is five and a half hours ahead of UTC.
Specify a proper time zone name. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "India/Kolkata" );
ZonedDateTime zdt = ZonedDateTime.now( z );
If you want to see that moment adjusted into UTC, just extract a Instant
which is a moment on the timeline always in UTC. Use Instant
objects for your logging, data storage, and data exchange (generally speaking).
Instant instant = zdt.toInstant();
To generate a String representing the date-time value in standard ISO 8601 format, simply call toString
.
You can generate strings in any format you desire. But usually best to let java.time localize automatically. Just specify the appropriate Locale
to indicate (a) the human language to use in translation, and (b) the cultural norms to decide issues such as capitalization, abbreviation, and punctuation.
Locale l = Locale.CANADA_FRENCH ; // Or Locale.US, Locale.ITALY, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( l );
String output = zdt.format( f );
Note that Locale
has nothing to do with time zone. Here we are taking the current wall-clock time of India and displaying in a format expected by a Québécois person.
Convert
While you should avoid the old legacy date-time classes because of their confusion, poor design, and flaws, sometimes you may need to interoperate with old code not yet updated to java.time types. For such cases you can convert to/from java.time by calling new methods added to the old classes.
java.util.Date utilDate = java.util.Date.from( zdt.toInstant() );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.