Javaでdd/MM/yyyy形式の文字列をdd/MM/yyyy形式の日付に解析するにはどうすればよいですか?

WBOY
リリース: 2023-08-29 19:13:06
転載
1524 人が閲覧しました

Javaでdd/MM/yyyy形式の文字列をdd/MM/yyyy形式の日付に解析するにはどうすればよいですか?

java.text パッケージには、希望する方法 (ネイティブ) で日付の書式設定と解析を行うための SimpleDateFormat というクラスが用意されています。

このクラスのコンストラクターの 1 つは、目的の日付形式を表す文字列値とコンストラクター SimpleDateFormat オブジェクト を受け入れます。

このクラスの format() メソッドは、 java.util.Date オブジェクトを受け取り、次の形式で日付/時刻文字を返します。現在のオブジェクト文字列で表されます。

したがって、日付文字列を別の日付形式に解析します -

  • 入力日付文字列を取得します。

  • これを java.util.Date オブジェクトに変換します。

    >
  • 必要な (新しい) 形式を文字列としてコンストラクターに渡して、SimpleDateFormat クラスをインスタンス化します。

  • 上で取得した Date オブジェクトをパラメータとして渡して、format() メソッドを呼び出します。

ライブ デモンストレーション

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class FormattingDate {
   public static Date StringToDate(String dob) throws ParseException {
      //Instantiating the SimpleDateFormat class
      SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      //Parsing the given String to Date object
      Date date = formatter.parse(dob);
      System.out.println("Date object value: "+date);
      return date;
   }
   public static void main(String args[]) throws ParseException {
      //Reading name and date of birth from the user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your name: ");
      String name = sc.next();
      System.out.println("Enter your date of birth (dd-MM-yyyy): ");
      String dob = sc.next();
      //Converting String to Date
      Date date = FormattingDate.StringToDate(dob);
      System.out.println("Select format: ");
      System.out.println("a: MM-dd-yyyy || b: dd-MM-yyyy || c: yyyy-MM-dd ");
      char ch = sc.next().toCharArray()[0];;
      switch (ch) {
         case 'a':
            System.out.println("Date in the format: MM-dd-yyyy");
            System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(date));
            break;
         case 'b':
            System.out.println("Date in in the format: dd-MM-yyyy");
            System.out.println(new SimpleDateFormat("dd-MM-yyyy").format(date));
            break;
         case 'c':
            System.out.println("Date in the format: yyyy-MM-dd");
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
            break;
         default:
            System.out.println("Model not found");
            break;
      }
   }
}
ログイン後にコピー

出力

Enter your name:
Krishna
Enter your date of birth (dd-MM-yyyy):
26-09-1989
Date object value: Tue Sep 26 00:00:00 IST 1989
Select format:
a: MM-dd-yyyy || b: dd-MM-yyyy || c: yyyy-MM-dd
a
Date in the format: MM-dd-yyyy
09-26-1989
ログイン後にコピー

以上がJavaでdd/MM/yyyy形式の文字列をdd/MM/yyyy形式の日付に解析するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:tutorialspoint.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!