java.text パッケージ内に存在する DateFormat クラスは、任意のロケールの日付をフォーマットおよび解析するために使用される抽象クラスです。これにより、日付をテキストにフォーマットし、テキストを日付に解析することができます。 DateFormat クラスは、デフォルトの日付/時刻を取得、フォーマット、解析するための多くの機能を提供します。 DateFormat クラスは Format クラスを拡張します。つまり、Format クラスのサブクラスです。 DateFormat クラスは抽象クラスであるため、言語に依存しない方法で日付や時刻を書式設定および解析する日付/時刻書式設定サブクラスに使用できます。
の フォーマット() Java の DateFormat クラスのメソッドは、指定された日付を日付/時刻文字列にフォーマットするために使用されます。基本的に、このメソッドは、この日付と時刻を特定の形式 (mm/dd/yyyy) に変換するために使用されます。
構文:
public final String format(Date date)>
パラメーター: このメソッドは、Date オブジェクト型の 1 つのパラメーター date を受け取り、文字列出力が生成される日付を参照します。
戻り値の型: mm/dd/yyyy の文字列形式で日付または時刻を返します。
gimp jpeg として保存
例 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);> >}> }> |
Cプログラム
>
>出力:
The original Date: Wed Mar 27 11:12:29 UTC 2019 Formatted Date: Mar 27, 2019>
例 2:
ジャワ
私のコンピュータの画面サイズはどれくらいですか
文字列トークナイザーJava
// 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>