Date Pattern para esta data (Jun 21, 03:00 2007 20:17)
2 respostas
paulovittor23
Bom dia !
Estou tentando criar uma data a partir de uma string que retiro de um texto por uma regex, porém a data vem com um timzone que eu não to conseguindo parsear para data…
fiz o seguinte:
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, ZZZZ yyyy HH:mm");
Date criacao = null;
try {
criacao = formatter.parse( matcherData.group() );//matcherData.group() me retorna "Jun 21, 03:00 2007 20:17"
} catch ( ParseException e ) {
e.printStackTrace();
}
Mas tá dando algum problema pois recebo uma excessão…
java.text.ParseException: Unparseable date: “Jun 21, 03:00 2007 20:17”
fiz um teste com uma data e mandei formatar p este pattern que eu to usando e o timezone sai de uma forma diferente (0300) …alguém sabe que pattern usar p sair 03:00 ?
Você não pode usar Z, já que ele é um timezone segundo RFC 822 ( ftp://ftp.rfc-editor.org/in-notes/rfc2822.txt )
O “quebra-galho” que eu indicaria seria remover manualmente o primeiro “:” que você encontrar e então fazer o “parse”. Argh!
3.3.DateandTimeSpecificationDateandtimeoccurinseveralheaderfields.Thissectionspecifiesthesyntaxforafulldateandtimespecification.Thoughfoldingwhitespaceispermittedthroughoutthedate-timespecification,itisRECOMMENDEDthatasinglespacebeusedineachplacethatFWSappears(whetheritisrequiredoroptional);someolderimplementationsmaynotinterpretotheroccurrencesoffoldingwhitespacecorrectly.date-time=[ day-of-week "," ]dateFWStime[CFWS]day-of-week=([FWS]day-name)/obs-day-of-weekday-name="Mon"/"Tue"/"Wed"/"Thu"/"Fri"/"Sat"/"Sun"date=daymonthyearyear=4*DIGIT/obs-yearmonth=(FWSmonth-nameFWS)/obs-monthmonth-name="Jan"/"Feb"/"Mar"/"Apr"/"May"/"Jun"/"Jul"/"Aug"/"Sep"/"Oct"/"Nov"/"Dec"day=([FWS]1*2DIGIT)/obs-daytime=time-of-dayFWSzonetime-of-day=hour":"minute[ ":" second ]hour=2DIGIT/obs-hourminute=2DIGIT/obs-minutesecond=2DIGIT/obs-secondzone=(("+"/"-")4DIGIT)/obs-zoneThedayisthenumericdayofthemonth.Theyearisanynumericyear1900orlater.Thetime-of-dayspecifiesthenumberofhours,minutes,andoptionallysecondssincemidnightofthedateindicated.Thedateandtime-of-daySHOULDexpresslocaltime.ThezonespecifiestheoffsetfromCoordinatedUniversalTime(UTC,formerlyreferredtoas"Greenwich Mean Time")thatthedateandtime-of-dayrepresent.The"+"or"-"indicateswhetherthetime-of-dayisaheadof(i.e.,eastof)orbehind(i.e.,westof)UniversalTime.ThefirsttwodigitsindicatethenumberofhoursdifferencefromUniversalTime,andthelasttwodigitsindicatethenumberofminutesdifferencefromUniversalTime.(Hence,+hhmmmeans+(hh*60+mm)minutes,and-hhmmmeans-(hh*60+mm)minutes).Theform"+0000"SHOULDbeusedtoindicateatimezoneatUniversalTime.Though"-0000"alsoindicatesUniversalTime,itisusedtoindicatethatthetimewasgeneratedonasystemthatmaybeinalocaltimezoneotherthanUniversalTimeandthereforeindicatesthatthedate-timecontainsnoinformationaboutthelocaltimezone.Adate-timespecificationMUSTbesemanticallyvalid.Thatis,theday-of-the-week(ifincluded)MUSTbethedayimpliedbythedate,thenumericday-of-monthMUSTbebetween1andthenumberofdaysallowedforthespecifiedmonth(inthespecifiedyear),thetime-of-dayMUSTbeintherange00:00:00through23:59:60(thenumberofsecondsallowingforaleapsecond;see[STD12]),andthezoneMUSTbewithintherange-9959through+9959.
paulovittor23
rs, olha que a sua solução não é ruim não, eu tinha conseguido achar uma pior :roll:
p/ não deixar vc curioso eu vou contar…tire suas conclusões disso:
MMMdd,HH:mmyyyyHH:mm
detalhe que nessa solução qdo o tempo era negativo dava erro…q nojo!
ainda bem q tive vergonha e não consegui deixar isso no código…uahuha