从用户那里获取日期输入并将其另存为Java中的日期 [英] Take date input from user and save it as date in java
问题描述
我正在尝试以(dd/mm/yy)
格式接收用户的输入,将其保存为相同格式,然后将其与getter,setter一起传递给另一个类.我已经尝试过这种方式:
I'm trying to take input from user in (dd/mm/yy)
format, save it in same format and pass it with getter, setter to another class. I have tried this way:
这是我尝试过的:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
Guest gstObject = new Guest();
try {
System.out.println("Enter check-in date (dd/mm/yy):");
String cindate = input.next();
Date date1 = myFormat.parse(cindate);
gstObject.setIndate(date1);
} catch (ParseException e) {
e.printStackTrace();
}
}
使用此代码,我在(27012017或27 01 2017)中输入内容时会遇到异常:
With this code I'm getting an exception when I give input in (27012017 or 27 01 2017):
java.text.ParseException: Unparseable date: "27"
at java.text.DateFormat.parse(DateFormat.java:366)
at edu.lfa.GuestArrayList.main(GuestArrayList.java:49)
请指出我犯了什么错误.
Please point out what mistake I have made.
推荐答案
其他答案解决了如何正确使用Scanner
的问题.我将解决其他一些问题.
Other answers addressed how to use Scanner
correctly. I'll address some other issues.
您正在使用麻烦的旧日期时间类(现在已遗留),由java.time类取代.避免像瘟疫这样的java.util.Date
类.
You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes. Avoid the java.util.Date
class like the Plague.
此外,您不恰当地尝试用date + time类表示仅日期的值.
Also, you are inappropriately trying to represent a date-only value with a date+time class.
相反,请使用java.time.LocalDate
. LocalDate
类表示日期-仅值,没有时间和时区.
Instead, use java.time.LocalDate
. The LocalDate
class represents a date-only value without time-of-day and without time zone.
要解析用户的输入,请指定一种格式匹配方式,以完全匹配 用户的预期输入.
To parse the user's input, specify a formatting pattern to match exactly the expected input from your user.
DateTimeFormatter f = DateTimeFormatter.parse( "dd/mm/uuuu" );
解析用户输入.
LocalDate ld = LocalDate.parse( input , f );
在业务类Guest
上,使成员变量的类型为LocalDate
.
On your business class Guest
, make the member variable of type LocalDate
.
要保留LocalDate
,您可以序列化为标准 ISO 8601 格式的文本.只需致电toString
.
To persist the LocalDate
, you can serialize to text in standard ISO 8601 format. Simply call toString
.
String output = guest.getLocalDate().toString();
2017-01-23
2017-01-23
关于java.time
java.time 框架已内置在Java 8及更高版本中.这些类取代了麻烦的旧版日期时间类,例如 Calendar
,& SimpleDateFormat
.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time 项目,现在位于 java.time 类.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参见 Oracle教程.并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
在哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 和 SE 9 及更高版本
- 内置.
- 具有捆绑的实现的标准Java API的一部分.
- Java 9添加了一些次要功能和修复.
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- 许多java.time功能都向后移植到Java 6& 7在> ThreeTen-Backport 中./li>
- > ThreeTenABP 项目适应了<专门用于Android的em> ThreeTen-Backport (上面提到).
- 请参阅 如何使用ThreeTenABP… .
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
ThreeTen-Extra 项目使用其他类扩展了java.time.该项目为将来可能在java.time中添加内容提供了一个试验场.您可能会在这里找到一些有用的类,例如
Interval
,YearWeek
,YearQuarter
和更多.The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.这篇关于从用户那里获取日期输入并将其另存为Java中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!