PROGRAMING/JAVA

[JAVA] 날짜 변환

donghunl 2011. 4. 26. 09:16
반응형

 1. String to Date 형변환

String sDate = "2011/04/25 13:50";

Date newDate = new SimpleDateFormat("yyyyMMddHHmm").parse(sDate);
 


2. 날짜 비교


String sDate_1 = "2011/04/15 11:50";
String sDate_2 = "2011/04/25 13:50";

Date newDate_1 = new SimpleDateFormat("yyyyMMddHHmm").parse(sDate_1);
Date newDate_2 = new SimpleDateFormat("yyyyMMddHHmm").parse(sDate_2);

long ldiff = newDate_2.getTime() - newDate_1.getTime();
long ldiffDays = ldiff / (24*60*60*100);



 3. 날짜 포맷 변경


String sDate = "2011/04/25 13:50";

Date newDate = new SimpleDateFormat("yyyyMMddHHmm").parse(sDate);

// 02001.July.04 AD 12:08 PM
SimpleDataFormat sdf = new SimpleDataFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
String convDate = sdf.format(newDate);

 

Examples

The following examples show how date and time patterns are interpreted in the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time in the U.S. Pacific Time time zone.
Date and Time Pattern Result
"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" Wed, Jul 4, '01
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700
반응형

'PROGRAMING > JAVA' 카테고리의 다른 글

[JAVA] UTC/GMT Local Time 시간 변환  (2) 2011.06.03