0

My code:

TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar cal = (Calendar) Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
System.out.println("Gregorian cal time:"+cal.getTime());
System.out.println("utc time: "+simpleDateFormat.format(cal.getTime()));

Both the print statements are showing the same local time.

Arjun
  • 144
  • 2
  • 14
  • 2
    And why shouldn't they? you're formatting the same Date (`cal.getTime()`), using the same (default) timezone. So it's normal to get the same result. What are you trying to achieve? – JB Nizet Feb 19 '17 at 14:02
  • But, I want UTC time. As I mentioned the timezone, it should show UTC time. – Viswanath Alikonda Feb 19 '17 at 14:04
  • The only difference between your 2 print statements is that one of them gets some string formatting - nothing about the date actually has changed. – takendarkk Feb 19 '17 at 14:04
  • You mentioned the timezone when creating the Calendar object. This allows you to get, for example, the current hour in the UTC timezone when asking the calendar. But you're not doing that. You're transforming the Calendar to a Date (which is just an instant on the universal time line, without any timezone information). And you're formatting this Date in your default timezone. – JB Nizet Feb 19 '17 at 14:06
  • Then, what should be the right code. – Viswanath Alikonda Feb 19 '17 at 14:06
  • The right code to do what? What do you want to achieve? – JB Nizet Feb 19 '17 at 14:07
  • I want to achive UTC time. – Viswanath Alikonda Feb 19 '17 at 14:07
  • So, you want to print the current date and time, as it would be displayed on the clock if you lived in the UTC timezone, is that right? If so, create a date (new Date()). Create a SimpleDateFormat. Set the timezone of the SimpleDateFormat to UTC. And format the date using the SimpleDateFormat. Note that Date, Calendar and SimpleDateFormat shouldn't be used anymore if you're usign Java 8. Learn the new java.time classes. – JB Nizet Feb 19 '17 at 14:09
  • My apologies for asking such a silly question. I am new to Java. – Viswanath Alikonda Feb 19 '17 at 14:12
  • No problem. That's what SO is for (although there are probably dozens of questions asking more or less the same thing). – JB Nizet Feb 19 '17 at 14:13

3 Answers3

1

You need to set the TimeZone of your DateFormatter to "UTC"

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("utc time: "+simpleDateFormat.format(cal.getTime()));
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
0
LocalDateTime utc_time = LocalDateTime.now(ZoneId.of("UTC"));
System.out.println("Date: "+utc_time);

works fine

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • The Question asks for a value in UTC. So **the use of [`LocalDateTime`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html) here is incorrect**, as the purpose of that class is to intentionally have *no* offset or time zone. By definition, a `LocalDateTime` cannot be in UTC. A `LocalDateTime` is not even an actual moment, it is *not* a point on the timeline. Your variable name `LocalDateTime utc_time` is a contradiction in terms. – Basil Bourque Feb 23 '17 at 06:11
0

tl;dr

Instant.now()

Avoid legacy date-time classes

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes. The legacy classes are an awful mess, poorly designed, confusing, and flawed. Avoid them.

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now();

instant.toString(): 2017-02-23T03:30:58.431Z

The format of that string generated by toString is standard ISO 8601 format. The Z on the end is short for Zulu and means UTC.

That's it, you are done.

ZonedDateTime

If you want to see that same moment as the wall-clock time of some particular region, then apply a ZoneId to get a ZonedDateTime.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. 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( "Asia/Kolkata" );
ZonedDateTime zdt = instant.atZone( z );

zdt.toString(): 2017-02-23T09:00:58.431+05:30[Asia/Kolkata]

India is five and a half hours ahead of UTC. So when adjusted into Asia/Kolkata time zone, note how the time-of-day jumps from 03:30 to 09:00.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

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, andfz more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154