我知道这个问题有一些答案,但我仍然感到困惑。 这个问题直接来自我的任务。
编写一个程序,以[{1}}格式为String
读取date
,并以month/day/year
格式显示,day.month.year
是{Europe
中使用的典型格式1}}。例如,如果输入为06/17/11
,则输出应为17.06.11.
您的程序应使用JOptionPane
进行输入和输出。“
请帮忙!
问候 埃弗特
答案 0 :(得分:1)
您需要2个DateFormats。一个用于inputFormat,另一个用于输出格式。 inputFormat解析输入并创建一个Date对象,outputFormat可以使用它来创建表示格式的String:
String input = JOptionPane.showInputDialog("Insert Date in Form MM/dd/yy");
DateFormat inputFormat = new SimpleDateFormat("MM/dd/yy");
DateFormat outputFormat = new SimpleDateFormat("dd.MM.yy");
Date date = inputFormat.parse(input);
String formattedDate = outputFormat.format(date);
JOptionPane.showMessageDialog(null, formattedDate);