-1

I am not able to get current india time.

I want to get current time for india, i updated my system clock after 5 minute then current time, so my system clock is running 5 minute earlier than actual india time but i want to get standard time for india.

current time is 11:05:06 and machine time is 11:10:06

public class TestDate {

    public static void main(String[] args) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
        Date date = new Date();
        sd.setTimeZone(TimeZone.getTimeZone("IST"));
        System.out.println(sd.format(date));
    }
}

output : 2019.02.12 AD at 11:10:06 IST
expected : 2019.02.12 AD at 11:05:06 IST

but i am getting wrong output , so please suggest

Bharti Ladumor
  • 1,624
  • 1
  • 10
  • 17
  • I recommend [this answer to the linked original question](https://stackoverflow.com/a/36916551/5772882). – Ole V.V. Feb 11 '19 at 09:26
  • 1
    I recommend you don’t use `SimpleDateFormat`, `TimeZone` and `Date`. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use `ZonedDateTime`, `ZoneId` and `DateTimeFormatter`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 11 '19 at 09:29
  • 2
    Is your system time correct for the time zone that you are in? If not, you should sync it. If you can't, then you'll need to use a web api somewhere to get the correct time. – TiiJ7 Feb 11 '19 at 09:47
  • @TiiJ7 yeah that's the real "problem", now that I understood – LppEdd Feb 11 '19 at 09:49
  • no my current local time is before 5 minute then my local time – Bharti Ladumor Feb 11 '19 at 09:49
  • 1
    You will ALWAYS get the time of the machine the command is run on, period. Only way to get some other time is to call a time service, like an NTP server, and request from it. – jwenting Feb 11 '19 at 11:05
  • What is “machine time”? This Question makes no sense as currently written. – Basil Bourque Feb 11 '19 at 19:30
  • yes, @jwenting i used NTP server and timezone as india and it worked for me !! – Bharti Ladumor Feb 14 '19 at 05:02

2 Answers2

2

The Date API has been superseded by a new JodaTime-like API in Java 8.
Use a ZonedDateTime object.

final ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));

This will give you the output 2019-02-11T14:49:28.625+05:30[Asia/Kolkata]

To format a Temporal use a DateTimeFormatter.

DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                 .format(now);
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • please try to understand my question i want to get global time for india and by this i m getting my local machine time, local machine time is variant user by user and i want to get global time – Bharti Ladumor Feb 11 '19 at 09:46
  • 2
    @BhartiLadumor no, here you're getting the Asia/Kolkata time, not your local time. What's global time? – LppEdd Feb 11 '19 at 09:47
  • my local time is before 5 minute then global time. my machine time is change 5 minute eariler – Bharti Ladumor Feb 11 '19 at 09:50
  • @BhartiLadumor 5 minutes do not represent a timezone. Your computer clock is just not synchronized. – LppEdd Feb 11 '19 at 09:51
  • 1
    @BhartiLadumor: Do you mean that your computer's clock is inaccurate, and you want to get the current time from some source on a network? That's a *very* different matter to it being in a different time zone than the one you're interested in. – Jon Skeet Feb 11 '19 at 09:51
  • yes @JonSkeet my local machine time is not accurate, actually in my application i want to get global, not time of local machine which is variant user by user – Bharti Ladumor Feb 11 '19 at 09:53
  • @BhartiLadumor If the "local machine" time is not accurate and you distribute your application as a standalone executable JAR, than everything will work as expected, at least if you do not hardcode values. If the code is served via, e.g, a web application server, you just need to synchronize that machine/s clock. – LppEdd Feb 11 '19 at 09:56
  • @BhartiLadumor: I strongly suggest that you edit your question then. You've included references to "global time" and a specific time zone, as if this is really about time zones - and it's not at all, it's about a local clock that's out of sync vs network time. – Jon Skeet Feb 11 '19 at 10:37
1

UTC+05:30

This is not a timezone, it's just an offset. You need to use valid timezone in order to print the date in that timezone, e.g.: Asia/Kolkata.

Following should work:

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
    SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    gmtDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    //Current Date Time in GMT
    System.out.println("Current Date and Time in UTC time zone: " + gmtDateFormat.format(new Date()));
}

Also, Asia/Kolkata timezone is not always UTC+05:30, it depends on daylight saving. So, we should rather use timezone by name than offset.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 11 '19 at 19:30