如何 return 用户在 Java 中输入 dd/mm/yyyy 日期?

How to return a user input date as dd/mm/yyyy in Java?

用户需要输入任务的名称、日期、地点等。然后该程序将列出每个输出名称、日期、位置等的每个任务。

当用户输入日期时,日期的格式不是我需要的。

示例:

Enter a date: 21/07/2020 

输出:

Tue Jul 21 00:00:00 AEST 2020

期望的输出:

21/07/2020

到目前为止,这是我的代码:-我正在使用多个 类

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner; 

class Task{

private String theTitle;
private Date theDate;
private String theTime;
private String theLocation;
private String theDuration;
private String theCategory;
SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy");

Task(String title, Date date, String time, String location, String duration, String category) {
    
    theTitle = title;
    theDate = date;
    theTime = time;
    theLocation = location;
    theDuration = duration;
    theCategory = category;
    
}

public String getTitle() {
    
    return theTitle;
}

public Date getDate() {

    return theDate;
}

public String getTime() {
    
    return theTime;
}

public String getLocation() {
    
    return theLocation;
}

public String getDuration() {
    
    return theDuration;
}

public String getCategory() {
    
    return theCategory;
}

public String getItem() {
    
    return theTitle + ", " + theDate + ", " + theTime + ", " + theLocation + ", " + theDuration + ", " + theCategory;
}

}


public class ToDoList2 {

public Task myTaskObj;
SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy");
private static List<String> currentList = new ArrayList<String>();

public ToDoList2() {
    
}

public static void main (String[] args) throws ParseException {
    
    ToDoList listObj = new ToDoList();
    
    int menuItem = -1;
    while (menuItem != 3) {
        menuItem = listObj.printMenu();
        switch (menuItem) {
        case 1:
            listObj.addItem();
            break;
        case 2:
            listObj.showList();
            break;
        case 3: 
            System.out.println("Goodbye!");
        default:
            System.out.println("Enter a valid option");
        }
    }   
    
}

public int printMenu() {
    
    Scanner scanner = new Scanner(System.in);
    System.out.println();
    System.out.println("----------------------");
    System.out.println("Main Menu");
    System.out.println("----------------------");
    System.out.println("1. Add a task");
    System.out.println("2. List tasks");
    System.out.println("3. Leave the program");
    System.out.println();
    System.out.print("Enter choice: ");
    int choice = scanner.nextInt();
    
    return choice;
    
}

public void showList() {
System.out.println();
System.out.println("----------------------");       
System.out.println("To-Do List");
System.out.println("----------------------");
int number = 0;
for (String item : currentList) {
    System.out.println(++number + ". " + item);
}
System.out.println("----------------------");


}

public void addItem() throws ParseException {
System.out.println("Add a task");
System.out.println("----------------------");

System.out.print("Enter the task title: ");
Scanner scanner = new Scanner(System.in);
String title = scanner.nextLine();

System.out.print("Enter the task date (dd/mm/yyyy): ");
Scanner scanner2 = new Scanner(System.in);
Date date=format.parse(scanner2.next());


System.out.print("Enter the task time: ");
Scanner scanner3 = new Scanner(System.in);
String time = scanner3.nextLine();

System.out.print("Enter the task location: ");
Scanner scanner4 = new Scanner(System.in);
String location = scanner4.nextLine();

System.out.println("Enter the task duration (optional - press enter to skip): ");
Scanner scanner5 = new Scanner(System.in);
String duration = scanner5.nextLine();

System.out.println("Enter the task category (optional - press enter to skip): ");
Scanner scanner6 = new Scanner(System.in);
String category = scanner6.nextLine();

myTaskObj = new Task(title, date, time, location, duration, category);

String theItem = myTaskObj.getItem();

currentList.add(theItem);
System.out.println("Task Added!");

    }
}

我是 java 的新手,如有任何帮助,我们将不胜感激!

您已经定义了 simpleDateFormat,但似乎没有使用它。它会专门为您服务。

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(sdf.format(yourDate));

你需要在里面添加这个逻辑

public String getItem() {
    
    return theTitle + ", " + sdf.format(theDate) + ", " + theTime + ", " + theLocation + ", " + theDuration + ", " + theCategory;
}

由于您没有在那里指定任何内容,它在字符串连接时调用了 Date.toString() 方法。

将此添加到您的代码中以获得正确的格式

String pattern = "dd/MM/yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String formatedDate = simpleDateFormat.format(date);

我建议您从过时且容易出错的 java.util 日期时间 API 切换到 rich set of modern date-time API.

定义格式化对象:

DateTimeFormatter.ofPattern("dd/MM/yyyy")

…并使用相同的 DateTimeFormatter 对象来解析和生成文本。

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class Main {
    public static void main(final String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter date in the format dd/MM/yyyy: ");
        String dateStr = scanner.nextLine();

        // Define a formatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

        // Parse the date string to LocalDate using the formatter
        LocalDate theDate = LocalDate.parse(dateStr, formatter);

        // Whenever you need to display it, display it using the same formatter
        String backToStr = formatter.format(theDate);
        System.out.println(backToStr);
    }
}

样本运行:

Enter date in the format dd/MM/yyyy: 21/07/2020
21/07/2020

旁注: 我可以看到您为每个输入创建了一个 Scanner 的新实例。您应该只创建一个 Scanner 实例并为每个输入使用相同的实例。