如何添加到jtextfield中带有字符串数据类型的日期 [英] how to add number of days to the date given in a jtextfield with string data type
问题描述
我只想询问在给定日期添加日期。
我有一个jtexfield(txtStart)和另一个jtexfield(txtExpiry)。
我需要显示txtExpiry从txtStart的日期起102天后的日期。
我正在使用KEYRELEASED事件。在txtStart中输入后,额外的102天的日期将显示在txtExpiry中。
这是我的代码,但它仍然是错误的。
private void txtStartKeyReleased(java.awt.event.KeyEvent evt){
// TODO在此处添加您的处理代码:
//将日历设置为2007年1月1日
int a = Integer.parseInt(txtStart.getText());
日历calendar = new GregorianCalendar(a,a,a);
calendar.add(Calendar.DAY_OF_MONTH,102);
PrintCalendar(calendar);
}
private void PrintCalendar(日历日历){
//定义输出格式并打印
SimpleDateFormat sdf = new SimpleDateFormat(yyyy-mm-dd) ;
String date = sdf.format(calendar.getTime());
long add = Date.parse(date);
txtExpiry.setText(add); ----->这部分这里也有一个错误。
}
我的代码仍然不会在txtExpiry中生成日期。感谢提前
收到帮助后,这是正确的代码:
private void txtStartKeyReleased(java.awt.event.KeyEvent evt){
try {
Date date1;
date1 = new SimpleDateFormat(yyyy-MM-dd)。parse(txtStart.getText());
System.out.println(date1);
SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd);
日历cal = Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DATE,102);
String expDateString = sdf.format(cal.getTime());
txtExpiry.setText(expDateString);
} catch(ParseException ex){
Logger.getLogger(ClientInfo.class.getName())。log(Level.SEVERE,null,ex);
}
}
使用
yyyy-MM-dd
注意:资本MM
请参阅: SimpleDateFormat
现在,一旦你有日期实例,你可以使用日历
来执行日期算术
日历cal = Calendar.getInstance();
cal.setTime(parsedDate);
cal.add(Calendar.DATE,102);
String expDateString = dateFormatter.format(cal.getTime());
Good Day . I just wanna ask about adding days in a given date. I have a jtexfield (txtStart) and another jtexfield(txtExpiry). I need to display in txtExpiry the date after 102 days from the date in txtStart. I am using KEYRELEASED event. after i input in txtStart, the date with additional 102 days shall appear in txtExpiry.
here's my code but it's still erroneous.
private void txtStartKeyReleased(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
// set calendar to 1 Jan 2007
int a = Integer.parseInt(txtStart.getText());
Calendar calendar = new GregorianCalendar(a,a,a);
calendar.add(Calendar.DAY_OF_MONTH,102);
PrintCalendar(calendar);
}
private void PrintCalendar(Calendar calendar){
// define output format and print
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
String date = sdf.format(calendar.getTime());
long add = Date.parse(date);
txtExpiry.setText(add); -----> this part here also has an error.
}
my code still won't generate the date in txtExpiry. Thanks in advance
Here's the right code after receiving help:
private void txtStartKeyReleased(java.awt.event.KeyEvent evt) {
try {
Date date1;
date1 = new SimpleDateFormat("yyyy-MM-dd").parse(txtStart.getText());
System.out.println(date1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DATE, 102);
String expDateString = sdf.format(cal.getTime());
txtExpiry.setText(expDateString);
}catch (ParseException ex) {
Logger.getLogger(ClientInfo.class.getName()).log(Level.SEVERE, null, ex);
}
}
Use
yyyy-MM-dd
Note: Capital MM
See : SimpleDateFormat
Now, once you have the date instance you could use Calendar
to do the days arithmetic
Calendar cal = Calendar.getInstance();
cal.setTime(parsedDate);
cal.add(Calendar.DATE, 102);
String expDateString = dateFormatter.format(cal.getTime());
这篇关于如何添加到jtextfield中带有字符串数据类型的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!