본문 바로가기

Programming/과거포스팅

자바 날짜 포멧 변환 (java String date format change)

날짜 포멧 변환

rss 기사를 파싱하는중 날짜 포멧 변경할 일이 생겼다. 

그래서 메서드를 만든 메서드이다.

package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class HelloMain {

	public static void main(String[] args) {
		
		
		
		String fromDate = "Tue, 23 Dec 2014 12:35:00 +0900";
		String fromDateFormat = "EEE, d MMM yyyy HH:mm:ss Z";
		String changeFormat = "yyyy-mm-dd HH:mm";
		
		System.out.println(changeRSSDateFormat(fromDate, fromDateFormat, changeFormat));

	}
	public static String changeRSSDateFormat(String fromDate,String fromDateFormat, String changeFormat) {
		String resultDate = "";
		
		SimpleDateFormat transFormat = new SimpleDateFormat(fromDateFormat ,Locale.ENGLISH);
		try {
			Date toDate = transFormat.parse(fromDate);
			transFormat = new SimpleDateFormat(changeFormat);
			resultDate = transFormat.format(toDate);
			
		} catch (ParseException e) {
			e.printStackTrace(); 
		}
		return resultDate;
	}

}