Results 1 to 2 of 2
  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default get current utc date/time

    I am trying to get the current utc date/time, but it keeps printing out in local time:

    Java Code:
    		LocalDateTime dt = LocalDateTime.now();
    		ZonedDateTime zdt = dt.atZone(ZoneOffset.UTC);
    		
    		System.out.println("time: " + zdt);
    
                    // output:
                   time: 2014-06-02T13:37:55.705Z
    13:37 is local time. Utc time is 17:37.

    Any idea what I might be doing wrong?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: get current utc date/time

    If I understand the API correctly, the atZone() method simply associates a ZoneOffset with a LocalDateTime, it doesn't actually change the time. So your zdt variable holds the current time, just with UTC associated with it.

    I think you want something closer to this:

    ZonedDateTime zdt = ZonedDateTime.now(ZoneOffset.UTC);
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to display current date and time using system.out?
    By shaunythesheep in forum New To Java
    Replies: 1
    Last Post: 12-18-2013, 08:29 PM
  2. Replies: 5
    Last Post: 02-16-2012, 09:26 AM
  3. Replies: 1
    Last Post: 08-16-2010, 10:49 AM
  4. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  5. how to get the current date and time
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •