java 如何将出生日期的用户输入转换为日历单位类型以添加到参数化构造函数中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28008022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would I convert a user input for Date of Birth into a Calendar unit type to add to a parameterized constructor?
提问by darhdevil
Currently in Object-Oriented Programming here. I'm required to prompt a user to input several particulars, including a Date of Birth in the DD/MM/YYYY format. Then, these particulars will be used to construct a Customer object to add to an ArrayList. The Date of Birth must be in a Calendar type to be used to construct a new Customer object.
目前在这里面向对象编程。我需要提示用户输入几个详细信息,包括 DD/MM/YYYY 格式的出生日期。然后,这些细节将用于构造一个 Customer 对象以添加到 ArrayList。出生日期必须是用于构造新客户对象的日历类型。
Thing is, I don't have a lot of idea around Calendar. Here are my questions:
问题是,我对 Calendar 没有太多想法。以下是我的问题:
- How would I get a user input in a Calendar type?
- If what I just said is not achievable, what would?
- How do I prompt the user for a String date and then convert it to a Calendar?
- 我将如何获得日历类型的用户输入?
- 如果我刚才说的不能实现,那怎么办?
- 如何提示用户输入字符串日期,然后将其转换为日历?
I (kinda) know how to use date formatting, though I've no idea how to work around for a user input. Most of the stuffs I found on google and stack overflow doesn't relate to a user input (or it did and I just can't get my head around it). Here's my current code in question:
我(有点)知道如何使用日期格式,但我不知道如何处理用户输入。我在 google 和堆栈溢出上发现的大多数东西都与用户输入无关(或者它确实存在,但我无法理解它)。这是我当前有问题的代码:
Scanner inputReg = new Scanner(System.in);
DateFormat df = new SimpleDateFormat("DD/MM/YYYY");
String IC = "";
String name = "";
String date = "";
Calendar dob = Calendar.getInstance();
String tel = "";
System.out.print("Enter Date of Birth (DD/MM/YYYY ): ");
date = inputReg.nextLine();
dob.setTime(date); //and here I'm stuck
System.out.println(dob);
回答by user1428716
This works :
这有效:
DateFormat df = new SimpleDateFormat("d/M/yyyy");
df.getCalendar().setLenient(false);......
and when you come out of the parsing
当你从解析中出来时
Customer c = new Customer(IC, name, df.getCalendar(), tel);
Complete Code without Name etc
没有名字的完整代码等
Scanner inputReg = new Scanner(System.in);
**DateFormat df = new SimpleDateFormat("d/M/yyyy");
df.getCalendar().setLenient(false);**
System.out.print("Enter Date of Birth (DD/MM/YYYY): ");
Date theDate = null;
try {
String date=inputReg.nextLine();
System.out.println("date="+date+"@");
**theDate = df.parse(date);**
} catch (ParseException e) {
e.printStackTrace();
}
;
Calendar dob=df.getCalendar(); // Set this inside Customer
inputReg.close();
回答by Ofer Lando
What you need is probably something like this:
你需要的可能是这样的:
Scanner inputReg = new Scanner(System.in);
DateFormat df = new SimpleDateFormat("DD/MM/YYYY");
String IC = "";
String name = "";
String date = "";
Calendar dob = Calendar.getInstance();
String tel = "";
System.out.print("Enter Date of Birth (DD/MM/YYYY ): ");
date = inputReg.nextLine();
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
cal.setTime(sdf.parse(date));
System.out.println(dob);
回答by JClassic
Here is a version which returns a "date" object.
这是一个返回“日期”对象的版本。
Scanner inputReg = new Scanner(System.in);
System.out.print("Enter Date of Birth (DD/MM/YYYY): ");
Date theDate = null;
try {
theDate = new SimpleDateFormat("ddMMyyyy").parse(inputReg.nextLine().replaceAll("/", ""));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(theDate);
inputReg.close();
回答by Vignesh
Try this..hope it helps..
试试这个..希望它有帮助..
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class ToCalender {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
try {
cal.setTime(sdf.parse("18/01/2015"));
}
catch (ParseException e) {
e.printStackTrace();
}
System.out.println("dob : " + cal.getTime());
}
}