SimpleDateFormat - time zone question
in
Programming Questions
•
2 years ago
I'm having trouble understanding SimpleDateFormat. What i'm trying to do is read a date from an XML file then eventually use it as an X axis point on a graph. Obviously that means I need to get the time from a object that includes both time and date (for example, my data looks like this: 2011-05-07T14:12:25Z).
phi.lho advised using DateFormat which makes sense. However, I'm having trouble understanding the time zone part. For example, take the initial time data point from my file:
2011-05-07T12:06:19Z
The actual start time was 8:06 AM so clearly no time offset is provided by the device writing this file, thus GMT. So, how do I apply my time zone to the data?
Here's a snippet of code. Up to this point all that's happened is I've created an ArrayList called allTimes that simply contains all of the time/date data from an XML file.
You can see my commented lines where I'm trying to see how different statements (setTimeZone, etc.) change the output. I just can't seem to get anything from it.
Aside from that, feel free to criticize my conversions from ArrayList to Array to a string to whatever else I've done. I don't totally understand all of the type stuff. All I know is that when it doesn't work and the error complains about type, I change the type.
Thanks everyone!
phi.lho advised using DateFormat which makes sense. However, I'm having trouble understanding the time zone part. For example, take the initial time data point from my file:
2011-05-07T12:06:19Z
The actual start time was 8:06 AM so clearly no time offset is provided by the device writing this file, thus GMT. So, how do I apply my time zone to the data?
Here's a snippet of code. Up to this point all that's happened is I've created an ArrayList called allTimes that simply contains all of the time/date data from an XML file.
- Object temp[] = allTimes.toArray(); //convert allTimes from type ArrayList to type Array (don't know why. just like arrays better.)
String[] stringArray = Arrays.copyOf(temp, temp.length, String[].class);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'"); //create new date format
//format.setTimeZone(TimeZone.getTimeZone("GMT"));
for(int i=0; i<stringArray.length; i++){
// See if we can parse
try {
Date current = format.parse(stringArray[i]);
//println(i + ". " + stringArray[i]);
//println(current);
println(i+1 + ". " + current.toString());
}
catch(ParseException pe) {
println("ERROR: Cannot parse \"" + stringArray[i] + "\"");
}
You can see my commented lines where I'm trying to see how different statements (setTimeZone, etc.) change the output. I just can't seem to get anything from it.
Aside from that, feel free to criticize my conversions from ArrayList to Array to a string to whatever else I've done. I don't totally understand all of the type stuff. All I know is that when it doesn't work and the error complains about type, I change the type.
Thanks everyone!
1