java - Parsing an ISO 8601 string local date-time as if in UTC -


in java need make calendar object string in format:

yyyy-mm-dd't'hh:mm:ss 

this string set gmt time. here's code:

    public static calendar datedecode(string datestring) throws parseexception {     timezone t = timezone.gettimezone("gmt");     calendar cal = calendar.getinstance(t);     date = new simpledateformat("yyyy-mm-dd't'hh:mm:ss");     date d = date.parse(datestring);     cal.settime(d);     return cal; } 

and then:

calendar cal = calendar.getinstance();     try     {         cal = datedecode("2002-05-30t09:30:10");     } catch (parseexception e)     {         // todo auto-generated catch block         e.printstacktrace();     }     int month =  cal.get(calendar.month)+1; 

and following output:

timezone: gmt+00:00 date: 2002-5-30 time: 7:30:10 

which can see wrong since time provided in gmt , not cet. think happens think time provided in cet (which current timezone) , therefore converts time cet gmt , therefore deducts 2 hours final result.

could me this?

thanks

btw: not wish use jodatime different reasons.

here code out setting timzones before parsing them:

// sdf contains calendar object default timezone. date date = new date(); string formatpattern = ....; simpledateformat sdf = new simpledateformat(formatpattern);  timezone t1; timezone t2; .... .... // set calendar of sdf timezone t1 sdf.settimezone(t1); system.out.println(sdf.format(date));  // set calendar of sdf timezone t2 sdf.settimezone(t2); system.out.println(sdf.format(date));  // use 'caloft2' instance-methods specific info // time-of-day date 'date' in timezone t2. calendar caloft2 = sdf.getcalendar(); 

another similar question found might too: how set default time zone in java , control way date stored on db?

edit:

here great tutorial on java & dates too: http://www.tutorialspoint.com/java/java_date_time.htm


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -