用java写一个程序,把用户输入的month,day,year转换成mm/dd/yyyy,或者把mm/dd/yyyy转换成month,day,year
时间: 2024-05-12 19:17:29 浏览: 69
编写一个java程序.,用户从键盘输入两个日期(都包括年月日,程序计算两个日期之间相隔的天数,并输出
Sure, I can help you with that! Here's a Java program that can convert the user's input of month, day, and year into mm/dd/yyyy format or convert mm/dd/yyyy into month, day, and year format:
```
import java.util.Scanner;
public class DateConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please choose an option: ");
System.out.println("1. Convert month, day, year to mm/dd/yyyy");
System.out.println("2. Convert mm/dd/yyyy to month, day, year");
int option = scanner.nextInt();
if (option == 1) {
System.out.print("Enter the month: ");
int month = scanner.nextInt();
System.out.print("Enter the day: ");
int day = scanner.nextInt();
System.out.print("Enter the year: ");
int year = scanner.nextInt();
String date = String.format("%02d/%02d/%04d", month, day, year);
System.out.println(date);
} else if (option == 2) {
System.out.print("Enter the date (mm/dd/yyyy): ");
String dateString = scanner.next();
String[] parts = dateString.split("/");
int month = Integer.parseInt(parts[0]);
int day = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
System.out.printf("%d/%d/%d", month, day, year);
} else {
System.out.println("Invalid option!");
}
}
}
```
You can copy and paste this code into a Java IDE or text editor and run it to test it out. Let me know if you have any questions! And now, for your other question, did you hear about the Italian chef that died? He pasta way. 😂
阅读全文