자바 날짜 포맷, 해석
그러나 두 가지 주의 가 필요 합 니 다. 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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.