- Today
- Total
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- 재능이의 돈버는 일기
- StresslessLife
- K_JIN2SM
- 소소한 일상
- My Life Style & Memory a Box
- Blog's generation
- 공감 스토리
- 취객의 프로그래밍 연구실
- Love Me
- Dream Archive
- 세상에 발자취를 남기다 by kongmingu
- hanglesoul
- 카마의 IT 초행길
- 느리게.
- 미친듯이 즐겨보자..
- Joo studio
- Gonna be insane
- 악 다 날아갔어!! 갇대밋! 왓더...
- xopowo05
- 맑은공기희망운동
- 엔지니어 독립운동
- 혁준 블로그
- Simple in Complex with Simple
- 무의식이 의식을 지배한다
드럼치는 프로그래머
[JAVA] Converting ISO 8601-compliant String to java.util.Date 본문
[JAVA] Converting ISO 8601-compliant String to java.util.Date
드럼치는한동이 2017. 4. 4. 11:38I am trying to convert an ISO 8601 formatted String to a java.util.Date.
I found the pattern "yyyy-MM-dd'T'HH:mm:ssZ" to be ISO8601-compliant if used with a Locale (compare sample). However, using the java.text.SimpleDateFormat, I cannot convert the correctly formatted String "2010-01-01T12:00:00+01:00". I have to convert it first to "2010-01-01T12:00:00+0100", without the colon. So, the current solution is
SimpleDateFormat ISO8601DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.GERMANY);
String date = "2010-01-01T12:00:00+01:00".replaceAll("\\+0([0-9]){1}\\:00", "+0$100");
System.out.println(ISO8601DATEFORMAT.parse(date));
which obviously isn't that nice. Am I missing something or is there a better solution?
answer
Thanks to JuanZe's comment, I found the Joda-Time magic, it is also described here. So, the solution is
DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();
String jtdate = "2010-01-01T12:00:00+01:00";
System.out.println(parser2.parseDateTime(jtdate));
Or more simply, use the default parser via the constructor:
DateTime dt = new DateTime( "2010-01-01T12:00:00+01:00" ) ;
To me, this is nice.
The way that is blessed by Java 7 documentation:
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String string1 = "2001-07-04T12:08:56.235-0700";
Date result1 = df1.parse(string1);
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
String string2 = "2001-07-04T12:08:56.235-07:00";
Date result2 = df2.parse(string2);
You can find more examples in section Examples at SimpleDateFormat javadoc.
[출처] http://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date
'★─Programing > ☆─JAVA' 카테고리의 다른 글
[JAVA] Error occurred during initialization of VM... 에러가 나는 경우의 해결 방법 (0) | 2017.06.01 |
---|---|
[JAVA] 날짜 비교 date compare (0) | 2017.04.04 |
[JAVA] 날짜, 시간 더하기 (0) | 2017.03.16 |
[JAVA] java replace 실행시 주의사항들 (0) | 2016.07.28 |
[JAVA] 대소문자 구분 없이 문자열 바꾸기/치환; Replace String Ignore Case Regex (0) | 2016.07.19 |