Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

current time

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar.getInstance() can return current time. From there you can extract the day/hour/min, etc. By default, is the hour/min it returns EST or Pacific Time or Mountain time ? What controls that ? Is it true it is controlled by where this code is run ? e.g. if you run it in CA it may give you 2PM, while you run it in NC it gives you 5PM , though you run the code at same time.
 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,

Yes, the time returned will depend on where the code is run. As explained in the java docs of Calendar http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html

getInstance()
Gets a calendar using the default time zone and locale.

And you have other overloaded getInstance() methods(like http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#getInstance(java.util.TimeZone, java.util.Locale) ) which can be used if we need the time of a particular timezone irrespective of where the code is run(However, there might not be lots of use cases for such a requirement ).
 
Paul Clapham
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true that what you get from that Calendar object is in terms of the default timezone (which the JVM gets from your system settings). It's also true that you can set the Calendar object to any other timezone (via, naturally, the setTimeZone method) and then you can get the current time in terms of that timezone.
 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lokesh chenta wrote:... can be used if we need the time of a particular timezone irrespective of where the code is run(However, there might not be lots of use cases for such a requirement ).

Well, air traffic, the military, shipping and some scientific work e.g. astronomy all need to use a standard reference for time, usually GMT (or UTC or "Zulu" time). Some banking systems use GPS timings as the reference time for transactions because they are globally available and incredibly accurate. I think these times are based on UTC, so if you are comparing timestamps for international transactions with the local times e.g. in Shanghai and New York, then you'd need to switch between several time zones as well.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Calendar.getInstance() returns time based on where the code is run. Assuming I run the code in USA but may run it in different cities. How do I check if the current time is later than 2PM US Eastern Time ? How do I write a code snippet that no matter where I run the code in USA it always can compare against US Eastern time 2PM ? Assuming there is no international issue, only run it in USA.
 
Paul Clapham
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I said... set the timezone of the Calendar to the timezone for US Eastern time (I think its name is "America/New_York"), get the hour of the day from the calendar, and see if it's greater than or equal to 14.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, do you mean if I set time zone to US New York, then even if I run this code in California, it will print out the date and time in Eastern Time (ET) format and value ? e.g. if it is 2PM PST/ 5PM EST, after I set time zone to New York, it will shows "HOUR_OF_DAY" as 12 + 5 = 17 even if I run it in California ?
 
Mike Simmons
Ranch Hand
Posts: 3040
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Try it; it will work.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Like I said... set the timezone of the Calendar to the timezone for US Eastern time (I think its name is "America/New_York"), get the hour of the day from the calendar, and see if it's greater than or equal to 14.


Paul, I am near Boston, and when I run this code, I set it to CA time zone on purpose, but I still get

time = 2012-07-18-10.14.40 AM EDT

which means it still takes EDT as I am running it in East coast. it didn't take the timezone I input. Why ?

 
Mike Simmons
Ranch Hand
Posts: 3040
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're missing one important part:

Also, the calendar.setTimeZone() doesn't really do anything here - you don't do anything with the Calendar that depends on time zone. But you do do something with the DateFormat that depends on time zone. So you could remove the calendar.setTimeZone() call.
 
Winston Gutkowski
Bartender
Pie
Posts: 10243
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben oliver wrote:which means it still takes EDT as I am running it in East coast. it didn't take the timezone I input. Why ?

Because you need to set the timezone on the formatter. getTime() simply returns a number, so it has no knowledge of timezone. I have to admit that SDF could do with a format(Calendar) method though.

Winston

[Edit] Too late.
 
Paul Clapham
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben oliver wrote:Paul, I am near Boston, and when I run this code, I set it to CA time zone on purpose, but I still get

time = 2012-07-18-10.14.40 AM EDT

which means it still takes EDT as I am running it in East coast. it didn't take the timezone I input. Why ?


Because you originally said

From there you can extract the day/hour/min, etc.


Which to me meant you were using the Calendar.get(Calendar.MINUTE) method and so on. After that you asked questions about your code without showing it to us. It's not surprising that confusion reigned.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. Yes, I missed setting the time zone for SimpledateFormatter..

One more question --- After I do Calendar c = Calendar.getInstance(), I can associate different time zone with it. But when I pass this Calendar instance "c" to some other method, it really does not matter what time zone I has set for "c" before I pass it to that method, correct ? The content of "c" is the same regardless of the time zone it is set for, correct ?

Thanks.
 
Paul Clapham
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's correct. The Calendar object just contains a single value which is the number of milliseconds since some particular instant of time. The purpose of associating a time-zone with it is so that other objects can ask the Calendar object to interpret that number in terms of that time-zone.
 
Mike Simmons
Ranch Hand
Posts: 3040
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the content of c will be different, but the value you get from c.getTime() will not be different. There are numerous things you might do with c that would show a difference between a calendar with PDT and a calendar with EDT - but calling c.getTime() is not one of them. In the code you've shown, you really don't need a Calendar at all. If you just want the current time, new Date() does that directly, with no Calendar involved.

Now if you do need a Calendar for something, it probably is a good idea to set its time zone. (Contrary to what I said earlier.) Because it's generally hard to understand which methods depend on that time zone, and which don't. If you have a Calendar, you might as well set its time zone, in case you do something that depends on that. In this particular case, you didn't really need a Calendar at all, and you didn't really need its time zone. But that's hard to know for sure. When in doubt, you should probably explicitly set the time zone of any Calendar or DateFormat you use - unless you're sure that you want it to use your default time zone.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bear with me. Is it true that, regardless of time zone, the "c.getTimeInMillis()" always returns the same value no matter what time zone you use ?
 
Paul Clapham
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that we have had a thorough discussion on this matter, it's time for you to fly on your own. So why don't you go through the API documentation and see what your answer is to that question? Post back here with your answer and why you think it's the correct answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic