作者:卟媞1亽儚 | 来源:互联网 | 2023-08-25 17:21
I ' vebeenstrugglingforawhilewithispieceofcodeforanandroidappandican ' tgetthehangofit.I ' vereadandtriedeverysolutionifoution
watiwanttodoishaveafunctiontoconvertastringlike ' 17.08.2012053360353360193360760000 ' toautcdateandafunctionthatttaker
string value=' 17.08.2012053360353360193360760000 ';
date format df=newsimpledateformat (DD.mm.yyyy hh : mm : ss : sssssss );
托里
{
datetoday=df.parse(value );
}
catch (ParseException e )
{
e .打印任务跟踪(;
}
thisresultsin : today=17.08.2012073360413360593600000017 aug 201204336041336059 gmtwhicharebothwrong。
itriedsettingsdf ' stimezonetoutc,no luck。
anotherthingthatinoticed 3360 ifidodf.set lenient (false;
itgivesme : Java.text.parse exception : unparseabledate : ' 17.08.201205336035336033603:360193360000 '。
ifanyonecanprovidemewithsomeexplanations/sample code,iwouldbeverygrateful.thanksinadvance
theresultyouaregettingisabsolutelyright。
Let's analyze this:
17.08.2012053360353360193360760760000
17:天(17th ) )。
08:监视器(August )。
2012:年(2012 ) )。
05:hourofday(5am )。
3:微影机(:35 )。
19:secondofminute(:19 )。
700000: millisecondsofsecond (7,600,000 ) )。
Now,thewaythevmseesthisisthatyouaredeclaringthetimeofdayas 5336035336019 am,then adding 7,600,000 milliseconds toit.7,7,7 6 minutes,40 seconds.5336035336019 am 02:06336040=7:41:59 am (and0milliseconds ) ) ) )。 thisistheresultyouaregetting.(italsoappearsthatyouarenotsettingthetimezoneproperly,sothegmtstringis3hoursbehindyouresuresuly
ifyouwanttoretainthe :7600000、tomyknowledgethisisnotpossible.asthiscanbesimplifiedintoseconds、 thevmwillautomaticallyreduceitintotheothertimeincrements.thesss ) should be for storing values 1000。
I ' dsuggestyoucreateanewsimpledateformatforyouroutput; butrememberthatthemillisecondswillbeabsorbedintotheothertimes (sincetheyareallstoredasasinglelonginthedateobject )。