자바 날짜 포맷, 해석

2806 단어 자바시간.
날짜 의 포맷 기 는 Simple DateFormat 을 사용 합 니 다. DateFormat 을 계승 합 니 다.Simple DateFormat 은 사용자 에 게 더 많은 제어 기능 을 제공 하고 유연 합 니 다.
        그러나 두 가지 주의 가 필요 합 니 다. Simple DateFormat 을 사용 하여 문자열 날 짜 를 분석 할 때 try 코드 블록 을 넣 어 특별한 날짜 형식 에 이상 이 생기 지 않도록 해 야 합 니 다.Simple DateFormat 인 스 턴 스 를 가 져 올 때 DateFormat 의 getTimeInstance, getDateInstance 또는 getDateTimeInstance 。 new SimpleDateFormat , , , 。 를 사용 하 는 것 이 좋 습 니 다.   SimpleDateFormat : (SimpleDateFormat)DateFormat.getDateInstance()---> applyPattern(pattern)---> 、 format(Date)、parse(String)。
/**
 *    、       
 */
package com.date_test;

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

/**
 * @author wkupaochuan
 * @time Jun 14, 2012
 * @version V 1.0
 */
public class DateFormat_test {
	
	public static void main (String agrs[])
	{
		//      
		Date dNow = new Date();
		//        ,            、        
		SimpleDateFormat df = (SimpleDateFormat)DateFormat.getDateInstance();
		
		
		/*     ,   */
		//      
		df.applyPattern("yyyy-MM-dd HH:mm:ss");
		System.out.println("      :  " + df.format(dNow));
		//      
		df.applyPattern("yy-MM-dd HH:mm:ss");
		System.out.println("      :  " + df.format(dNow));
		//           
		df.applyPattern("MM-dd");
		System.out.println("           :  " + df.format(dNow));
		//  12     
		df.applyPattern("yyyy-MM-dd hh:mm:ss");
		System.out.println("  12     :  " + df.format(dNow));
		//         0  
		df.applyPattern("yyyy-m-dd h:mm:ss");
		System.out.println("         0  :  " + df.format(dNow));
		//       ,    、   ,    ''  
		df.applyPattern("yyyy' 'M' 'd' '
h' 'm' 'mm' '"); System.out.println(" , 、 :
" + df.format(dNow) + "


"); /* */ String dTest = "2012-09-10"; // 、 , try { /* Date , df , * * */ df.applyPattern("yyyy-MM-dd"); Date ddTest = df.parse(dTest); // , System.out.println(ddTest); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

좋은 웹페이지 즐겨찾기