File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes current time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "current time" Watch "current time" New topic
Author

current time

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
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

Joined: Oct 27, 2009
Posts: 100

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 ).


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

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

Joined: Mar 01, 2009
Posts: 1790
    
  16

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.


No more Blub for me, thank you, Vicar.
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
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
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

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

Joined: Mar 28, 2006
Posts: 375
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
Yes. Try it; it will work.
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Mar 17, 2011
Posts: 8060
    
  22

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.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

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

Joined: Mar 28, 2006
Posts: 375
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
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Mar 28, 2006
Posts: 375
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
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: current time