This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello everyone. I've been working on this for a while today and I've hit a wall as to what the issue is.
Basically, I need to convert to a local time from GMT time. I am using a Calendar object, (which will contain the time in GMT format that I need to convert to local time). I am using a date (String) formatted as yyyy-MM-dd that I need to use in the conversion process. I will also have the timezone I need to convert to (IE America/Chicago etc...). I've posted my code below but here is a summary of what I'm trying to do:
I need to convert the time in the Calendar Object to local time using the timezone I have and return a String with the time formated as HHmm. What I've been trying to do is to build a Date object by using SimpleDateFormat.parse() that will contain the date from the string, and the time from the calendar. I will then use SimpleDateFormat to format that date into the proper String with the correct converted local time. However, when I run this, it doesn't seem to convert my time. It looks like it's not using the Timezone I provide it, but keeps it at the default CENTRAL time (my local machine).
I pull the date String and build the SimpleDateFormat object as "yyyy-MM-ddTHHmm". After I build the SimpleDateFormat object, I set it's timezone to something like America/Los_Angeles and I'll put 0900 in my calendar and use 2010-03-05 for the date. I would exepect my 0900 to change to 0700 after my conversion, however it doesn't. It stays at 0900. I hope i've explained everything clearly enough, please let me know if I need to elaborate anymore.
Edited to remove un-necessary parts in code - can add them back if needed.
Joined: Oct 26, 2009
I've been playing around with this some more and have more details. I can see where the problem is, but I don't know what is causing it.
Can anyone explain why when I parse the new Date Object below (located in 2nd code structure below on this line
apptDateTime = sdfDateTime.parse(sb.toString());
: , my time changes from 09:01 to 11:01? The conversion to Pacific time looks to be working correctly it's just converting it from 11:01 and not the 09:01.
Here is the code I'm using to build my test case (With notes):
I then pass that calendar to my method which also takes in a date field to perform the logic to convert this to local time (in this example Pacific). Here is the code which does the conversion (with notes and findings):
That was very long and very wide so I didn't read all of it. But you seem to be under the impression that a Date has a time zone. It doesn't. Your comments suggest you are displaying a Date in some way and expecting that display to use some time zone other than your local time zone. It won't.
If you have a string which represents a timestamp in one time zone, and you want to change that to a string which represents a timestamp in another time zone, here's what you have to do:
Use that to format the Date object to a new String
Basically that's it. You don't need anything near as complicated as what you had there. In particular I don't think that Calendar objects are going to help.
Joined: Oct 26, 2009
Thanks for the reply Paul, I'll shorten everything down to where the issue is. Unfortunately, I have to use the Calendar object because that's what is being given to me.
I need to get the time from the calendar and that will give me the Date Object.
I then use SimpleDateFormat on the new date object to get me the time as "HHmm". I create a String buffer to concatenate the date I'm given (String) along with the time(hours and minutes) I just pulled out (via SimpleDateFormat) to parse a new date object.
the string buffer will have this value: "2010-03-05T0901".
I will then use another SimpleDateFormat object setup as "yyyy-MM-dd'T'HHmm" to parse the StringBuffer I just created above into a new date object.
Now in debug mode I can see the StringBuffer is set to 2010-03-05T0901, however, after I parse that into another date object, Debug is showing the time for that object as 11:01, which doesn't look right. I'm not sure where the extra 2 hours are coming from. I would think that the time in the date object would stay at 0901. Am I misunderstanding something?
Joined: Oct 26, 2009
So I guess the short question is, when I use SimpleDateFormat(yyyy-MM-dd'T'HHmm) to parse "2010-03-05T0901" the date I get back is "Fri Mar 05 11:01:00 CST 2010" and not "Fri Mar 05 09:01:00 CST 2010"?