02-11-2011 04:33 PM
With the following code I'm getting inconsistent time.
If I set the string to "12/15/2011 10:20 PM" I get back "12/15/2011 04:20 PM".
If I set the string to "12/15/2011 12:00 AM" I get back "12/15/2011 04:00 AM".
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
java.util.Calendar d2 = java.util.Calendar.getInstance();
String date = "12/15/2011 10:20 PM";
String ampm = date.substring(17,19);
mm = Integer.parseInt(date.substring(0, 2)) - 1; // january = 0
dd = Integer.parseInt(date.substring(3 ,5));
yy = Integer.parseInt(date.substring(6, 10));
hr = Integer.parseInt(date.substring(11,13));
mi = Integer.parseInt(date.substring(14,16));
long t2 = d2.getTime().getTime();
_testDateField = new DateField("Date: ", t2, df, DrawStyle.LEFT);
Solved! Go to Solution.
02-11-2011 05:49 PM
Probably going to have to call _testDateField.setTimeZone() and put it to the user's TimeZone. Working with the calendar and date objects can sometimes be a b**ch when you're trying to convert between GMT and you're time.
02-11-2011 08:08 PM
Agree with jproffit: if you search the forum you will find quite a bit of traffic on this subject. I know i have answered the question several times, and posted code examples for adjusting the time.
02-11-2011 08:25 PM
02-11-2011 10:41 PM
If you check the difference in the time, I think you'll find that the delta is the same as your time zone, i.e. the number of hours between your time zone and GMT.
Is this not the case?
02-12-2011 02:49 PM
02-14-2011 04:22 PM
Is this a time zone issue? How can you tell? Why wouldn't the time zone remain consistent from setting/getting time? How do I programatically set the time zone to get this to work properly?
02-14-2011 04:26 PM
Date and Calendar objects represent their time in GMT. Did you try doing DateField.setTimeZone(TimeZone.getDefault()); if you're trying to set it for now (this won't necessarily work if you are doing it for different dates).