问题描述
我花了很多时间阅读Java文档和相关问题,但仍然无法准确了解我需要做什么来完成我想要的。我知道我将使用 MaskFormatter
(因为我想要 / /
总是显示), SimpleDateFormater
和 DocumentListeners
所以我花了大部分时间研究这些,但无法弄清楚如何一起使用所有的东西。
我有一个 JFormattedTextField
使用 MaskFormatter(## / ## / ####)
我想最终以 MM / dd / yyyy
的格式存储为日期。
private JFormattedTextField weekEndingData = new JFormattedTextField(createFormatter(## / ## / ####));
private String dateString = weekEndingData.toString();
private SimpleDateFormat dateFormatter = new SimpleDateFormat(MM / dd / yyyy);
private MaskFormatter createFormatter(String s){
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
} catch(java.text.ParseException exc){
System.err.println(formatter is bad:+ exc.getMessage());
System.exit(-1);
}
return formatter;
}
此日期反映了我的情况下星期天的周结束日期。该日期将被发送到数据库,并且还将用于自动显示一周中剩余时间的日期。所以例如:
如果用户输入的周结束日期是3/30/2014,则表单上的JLabel将填写为3/30文字星期日,星期六下午3点29分,直到星期一。这让我进入下一个问题,减去日期。
我已经完成了研究,只是到了JodaTime的任何地方,但是我也看到有人提到新的Java8 JDK在日期方面,解决了Java以前的许多弱点。
我正在寻找指导方针,列出我想要完成的要求,因为我觉得很混乱,似乎找不到足够强的方向。我也希望有关JDK8使用日期的方式的任何反馈,如果它比Joda更好的选择。 (我知道主观性在这里皱着眉头,但我不知道还有什么要问的。)
编辑可视化可能有帮助。此专家组将被印刷,所以JSpinner是不利的。黄色只是暂时的,以帮助我可视化空间分配。
有更好的选择允许用户在Swing中输入日期,但在第三方库中:
-
JDateChooser
可在中使用 -
JXDatePicker
可在中使用
但是,如果您不想使用任何第三方库,而不是使用标准的Swing组件,建议您使用。请参阅教程。
此代码段应该足以使其正常工作:
SpinnerDateModel model = new SpinnerDateModel );
JSpinner spinner = new JSpinner(model);
JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner,MM / dd / yyyy);
DateFormatter formatter =(DateFormatter)editor.getTextField()。getFormatter();
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
spinner.setEditor(editor);
为了正常工作,我没有尝试Java 8,但有一个官方的踪迹:
如果您不想使用新的API,那么您可以使用(旧学院 API)。例如:
Date date =(Date)spinner.getModel()。getValue();
日历calendar = Calendar.getInstance();
calendar.setTime(date);
if(calendar.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY){
System.out.println(今天是星期天!);
}
如果你想减少一天,你可以做如下:
calendar.add(Calendar.DAY_OF_MONTH,-1);
这个问题也有很好的答案:
I've spent a lot of time reading Java docs and related questions but am still unable to understand exactly what I need to do to accomplish what I want.
I know that I will be using a MaskFormatter
(because I want / /
always displayed), SimpleDateFormater
and DocumentListeners
so I spent most of my time studying those but can't figure out how to use everything together.
I have a JFormattedTextField
that uses a MaskFormatter("##/##/####")
which I want to end up being stored as a Date in the format of MM/dd/yyyy
.
private JFormattedTextField weekEndingData = new JFormattedTextField(createFormatter("##/##/####"));
private String dateString = weekEndingData.toString();
private SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
private MaskFormatter createFormatter(String s) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}
This date reflects the "week ending date" which is Sunday in my case. This date will be sent to a database and will also be used to automatically display the dates of the rest of the days in the week. So for example:
If the week ending date entered by the user is 3/30/2014, then a JLabel on the form will fill in as 3/30 under the text "Sunday", and 3/29 under Saturday, and so on till monday. Which brings me to my next issue, subtracting dates.
I've done research and just about everywhere points towards JodaTime, but I also saw someone mention that the new Java8 JDK addresses much of the previous weak points for Java in terms of Dates.
I am looking for guidance in terms of mapping/listing out my requirements for what I want to accomplish because I feel very muddled and can't seem to find strong enough direction. I am also hoping for any feedback on the way JDK8 uses dates and if it is a better choice than Joda. (I know subjectiveness is frowned upon here but I don't know where else to ask.)
EDIT a visualization may help. This Panel will be printed so a JSpinner is unfavorable. The yellow is only there temporarily to help me visualize space distribution.
There are better choices to allow users input dates in Swing, but in third-party libraries:
JDateChooser
available in JCalendar libraryJXDatePicker
available in SwingX libary
However if you don't want to use any third-party library but standard Swing components instead, I'd suggest you use JSpinner with a date model. See How to Use Spinners tutorial.
This code snippet should be enough to get it work:
SpinnerDateModel model = new SpinnerDateModel();
JSpinner spinner = new JSpinner(model);
JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "MM/dd/yyyy");
DateFormatter formatter = (DateFormatter)editor.getTextField().getFormatter();
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
spinner.setEditor(editor);
In order to work with dates honestly I didn't try Java 8 yet, but there's an official trail: Trail: Date Time
If you don't want to use the new API then you can use Calendar API (the old school API). For instance:
Date date = (Date)spinner.getModel().getValue();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
System.out.println("Today is Sunday!");
}
If you want to substract a day you can do as follows:
calendar.add(Calendar.DAY_OF_MONTH, -1);
There are good answers in this question too: How can I increment a date by one day in Java?
这篇关于如何使用掩码从JFormattedTextField获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!