17

In Java 8 Instant.now() method showing wrong time . My code looks like :

import java.time.*;
import java.time.temporal.*;
public class DateTimeDemo{
    public static void main(String []args){
            Instant now = Instant.now();
            System.out.println(now);
        }

    }

Its date part is correct but time part .

eg

2016-07-11T11:01:25.498Z but in my system it is 4.31 PM

I am using Asia/Calcutta TimeZone

optional
  • 3,260
  • 5
  • 26
  • 47

1 Answers1

50

That's the correct time. Note the "Z" at the end, indicating UTC - 11:01:25 in UTC is 16:31:25 in Asia/Calcutta.

If you want to represent "now" in your default time zone, use ZonedDateTime.now() instead.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 15
    The `Z` is short for `Zulu` which means [UTC](https://en.m.wikipedia.org/wiki/Coordinated_Universal_Time). – Basil Bourque Jul 11 '16 at 17:35
  • @BasilBourque I don't think "z" is short for "zulu". From that page "z" is used because UTC is the "zero" timezone and "zulu" is just the NATO phonetic alphabet word for "z". – mowwwalker Feb 22 '21 at 22:48
  • @mowwwalker Same difference to me. I prefer the brevity. Furthermore, the modern [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) standard used by industry does not recognize the [military/aviation zones A-Z](https://en.wikipedia.org/wiki/List_of_military_time_zones), so the distinction is not relevant to most of us. – Basil Bourque Feb 23 '21 at 00:09