【问题标题】:Print date from input ID user in java从java中的输入ID用户打印日期
【发布时间】:2022-02-02 04:29:08
【问题描述】:

我必须制作一个代码,输入用户名,然后输入一个基于格式的 ID 号(1102199344556699 - 其中前 8 个字符是他的出生日期)。 我的代码:

import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {

        //user input
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your name: ");
        int length = scanner.nextLine()
                .length();
        System.out.println(length);

        //user id input
        Scanner scanner2 = new Scanner(System.in);
        System.out.println("Enter your id number");
        String idNUmber = scanner2.nextLine();
        if (idNUmber.length() < 11) {
            throw new InvlaidIdException("invalid");
        } else {
        String dateOfBirth = idNUmber.substring(0, 8);

    //found this way,on the output is giving me from input -110219935656525
   //output is ok but as you can see on My output i get some extra things (please see OUTPUT)
        TemporalAccessor date = DateTimeFormatter.ofPattern("ddMMyyyy").withLocale(Locale.FRANCE).parse(dateOfBirth);
            System.out.println(date);
                }
            }
        }

输入用户 ID -110219935656525

输出:

This is  the birthday date: {},ISO resolved to 1993-02-11

我怎样才能摆脱 {},ISO 解析为? 谢谢,

【问题讨论】:

  • 您想使用LocalDate,而不是TemporalAccessor。只需LocalDate date = LocalDate.parse(dateOfBirth, DateTimeFormatter.ofPattern("ddMMyyyy", Locale.FRANCE));。顺便说一句,重复使用相同的 Scanner,不要创建第二个。
  • 非常感谢您的贡献????。你解释的我也试试看????
  • 效果很好:)

标签: java date java-17


【解决方案1】:

经过更多研究 我得到了我需要的输出:)

这里是完整的代码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {

        //user input
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your name: ");
        int length = scanner.nextLine()
                .length();
        System.out.println(length);

        //id user input
        Scanner input = new Scanner(System.in);
        String civilIdStr = "";
        System.out.println("Enter your id number");
        civilIdStr = input.nextLine();
        if (civilIdStr.length() < 11) {
            throw new InvlaidIdException("invalid");
        } else {
// sout the date from the Id user input  (format DD-MM-YY)
                String dob = civilIdStr.substring(0, 8);
                System.out.println("Date of birth from user ID: " +
                        dob.substring(0, 2) + "/" +   //DATE
                        dob.substring(2, 4) + "/" +   //MONTH
                        dob.substring(6, 8));         //YEAR
            }
        }
    }

输出:

Enter your name: 
Victor
6
Enter your id number
11021993554644
Date of birth from user ID: 11/02/93

【讨论】:

    猜你喜欢
    • 2019-05-22
    • 2019-10-28
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多