0

How can I get UTC value in Java of any given time and date with the respective time-zone?

Say for example my current time zone is Asia/Kolkata, now how can I get UTC value of say 1.00 am on 21/07/2018?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 5
    this can't possibly be a not-duplicate... – lelloman Jul 26 '18 at 10:34
  • 1
    @lelloman Indeed.. I was looking at https://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java – Romeo Sierra Jul 26 '18 at 10:35
  • 1
    You want to input a date and specify your timezone, and be able to see what UTC time correpond ? – azro Jul 26 '18 at 10:46
  • 1
    yes exactly I want to give inputs @azro – user2396066 Jul 26 '18 at 10:50
  • 1
    Edit your question with an example of : given input and expectd output – azro Jul 26 '18 at 10:57
  • Possible duplicate or near-duplicate of (1) [How to get time from user with respect to timezone](https://stackoverflow.com/questions/50052157/how-to-get-time-from-user-with-respect-to-timezone), (2) [TimeStamp.valueOf() method](https://stackoverflow.com/questions/49809816/timestamp-valueof-method) and/or (3) [How can I get the current date and time in UTC or GMT in Java?](https://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java) – Ole V.V. Jul 26 '18 at 11:13
  • 2
    Welcome to Stack Overflow. [As the help page says](https://stackoverflow.com/help/how-to-ask), you are supposed to search and research before asking. You are also likely to find a good answer faster that way. – Ole V.V. Jul 26 '18 at 11:21
  • @user2396066 Mark correct answer –  Aug 13 '18 at 09:26

3 Answers3

2

For getting currect time in UTC.

 Instant.now()  // Current time in UTC.

For getting current time in any desired TimeZone.

 ZonedDateTime.now( ZoneId.systemDefault() )   // Current time in your ZoneId.

Kolkata Example :

ZoneId zoneKolkata = ZoneId.of( "Asia/Kolkata" ) ;  
ZonedDateTime zoneDTKolkata = instant.atZone( zoneKolkata ) ;

To adjust back to UTC, extract an Instant from the ZonedDateTime.

Instant instant = zoneDTKolkata.toInstant() ;

You can adjust from UTC to a time zone.

ZonedDateTime zoneDTKolkata = instant.atZone( zoneKolkata ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Use the Java 8 time API instead of the older API (ie Date & SimpleDateFormat solution proposed by rajadilipkolli)

// System time (ie, your operating system time zone)
LocalDateTime ldt = LocalDateTime.of(year, month, day, hour, minute, second);

// Time in Asia/Kolkata
ZonedDateTime kolkata = ldt.atZone(ZoneId.of("Asia/Kolkata"));

// Time in UTC
OffsetDateTime utc = ldt.atOffset(ZoneOffset.UTC);
klonq
  • 3,535
  • 4
  • 36
  • 58
  • 1
    That will give the same wall-clock time in UTC. I thought the asker was after the same point in time, but s/he can better tell. – Ole V.V. Jul 26 '18 at 12:50
0
    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern("h.mm a 'on' dd/MM/uuuu")
            .toFormatter(Locale.ENGLISH);
    ZoneId zone = ZoneId.of("Asia/Kolkata");
    String localDateTimeString = "1.00 am on 21/07/2018";
    Instant i = LocalDateTime.parse(localDateTimeString, formatter)
            .atZone(zone)
            .toInstant();
    System.out.println("UTC value is: " + i);

This prints:

UTC value is: 2018-07-20T19:30:00Z

I wasn’t sure whether you needed to parse the exact string you gave, 1.00 am on 21/07/2018, into a date-time object, but in case I have shown how. The challenge is that am is in lowercase. In order to specify case insensitive parsing I needed to go through a DateTimeFormatterBuilder.

As you can see, the code converts to an Instant, which is the modern way to represent a point in time in Java. Instant.toString always prints the time in UTC. The Z at the end means UTC. If you want a date-time that is more explicitly in UTC you may use

    OffsetDateTime odt = LocalDateTime.parse(localDateTimeString, formatter)
            .atZone(zone)
            .toInstant()
            .atOffset(ZoneOffset.UTC);
    System.out.println("UTC value is: " + odt);

The output is similar, only OffsetDateTime leaves out the seconds if they are 0 (zero):

UTC value is: 2018-07-20T19:30Z

Link: Oracle tutorial: Date Time explaining how to use java.time, the modern Java date and time API.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161