logo

예제가 포함된 Java의 DateFormat format() 메서드

java.text 패키지 내에 있는 DateFormat 클래스는 모든 로케일의 날짜 형식을 지정하고 구문 분석하는 데 사용되는 추상 클래스입니다. 이를 통해 날짜를 텍스트로 형식화하고 텍스트를 날짜로 구문 분석할 수 있습니다. DateFormat 클래스는 기본 날짜/시간을 가져오고, 형식을 지정하고, 구문 분석하는 많은 기능을 제공합니다. DateFormat 클래스는 Format 클래스를 확장합니다. 이는 Format 클래스의 하위 클래스임을 의미합니다. DateFormat 클래스는 추상 클래스이므로 언어 ​​독립적인 방식으로 날짜 또는 시간의 형식을 지정하고 구문 분석하는 날짜/시간 형식 지정 하위 클래스에 사용할 수 있습니다.

그만큼 체재() Java의 DateFormat 클래스 메소드는 주어진 날짜를 날짜/시간 문자열로 형식화하는 데 사용됩니다. 기본적으로 이 방법은 이 날짜와 시간을 mm/dd/yyyy와 같은 특정 형식으로 변환하는 데 사용됩니다.



통사론:

public final String format(Date date)>

매개변수: 이 메소드는 Date 객체 유형의 날짜 매개 변수 하나를 사용하고 문자열 출력이 생성될 날짜를 참조합니다.

반환 유형: mm/dd/yyyy 문자열 형식으로 날짜 또는 시간을 반환합니다.



캐스트 SQL

예시 1:

연결리스트 자바

자바






// Java Program to Illustrate format() Method> // of DateTime Class> // Importing required classes> import> java.text.*;> import> java.util.Calendar;> // Main class> // DateFormat_Demo> public> class> GFG {> >// Main driver method> >public> static> void> main(String[] args)> >{> >// Initializing the first formatter> >DateFormat DFormat = DateFormat.getDateInstance();> >// Initializing the calendar Object> >Calendar cal = Calendar.getInstance();> >// Displaying the actual date> >System.out.println(>'The original Date: '> >+ cal.getTime());> >// Converting date using format() method> >String curr_date = DFormat.format(cal.getTime());> >// Printing the formatted date> >System.out.println(>'Formatted Date: '> + curr_date);> >}> }>

중위순회
>

>

스프링 초기화
산출:

The original Date: Wed Mar 27 11:12:29 UTC 2019 Formatted Date: Mar 27, 2019>

예시 2:

자바




포토샵에서 다시 실행하는 방법
// Java Program to Illustrate format() Method> // of DateTime Class> // Importing required classes> import> java.text.*;> import> java.util.*;> // Main class> // DateFormat_Demo> public> class> GFG {> >// Main driver method> >public> static> void> main(String[] args)> >{> >// Initializing the first formatter> >DateFormat DFormat = DateFormat.getDateTimeInstance(> >DateFormat.LONG, DateFormat.LONG,> >Locale.getDefault());> >// Initializing the calendar Object> >Calendar cal = Calendar.getInstance();> >// Displaying the actual date> >System.out.println(>'The original Date: '> >+ cal.getTime());> >// Converting date using format() method and> >// storing date in a string> >String curr_date = DFormat.format(cal.getTime());> >// Printing the formatted date on console> >System.out.println(>'Formatted Date: '> + curr_date);> >}> }>

>

>

산출

The original Date: Tue Jan 11 05:42:29 UTC 2022 Formatted Date: January 11, 2022 at 5:42:29 AM UTC>