Java 如何将天数添加到具有字符串数据类型的 jtextfield 中给定的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9690364/
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 to add number of days to the date given in a jtextfield with string data type
提问by zairahCS
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.
再会 。我只是想问在给定日期添加天数。我有一个 jtexfield (txtStart) 和另一个 jtexfield(txtExpiry)。我需要在 txtExpiry 中显示从 txtStart 中的日期算起 102 天后的日期。我正在使用 KEYRELEASED 事件。在txtStart 中输入后,txtExpiry 中将出现额外102 天的日期。
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
我的代码仍然不会在 txtExpiry 中生成日期。提前致谢
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);
}
}
采纳答案by Jigar Joshi
Use
用
yyyy-MM-dd
Note: Capital MM
注:大写MM
See : SimpleDateFormat
看 : SimpleDateFormat
Now, once you have the date instance you could use Calendar
to do the days arithmetic
现在,一旦你有了日期实例,你就可以用Calendar
它来做天数算术
Calendar cal = Calendar.getInstance();
cal.setTime(parsedDate);
cal.add(Calendar.DATE, 102);
String expDateString = dateFormatter.format(cal.getTime());
回答by MadProgrammer
Once you have the correct format, you could use JodaTime. You could parse the String
value directly using JodaTime, but since you've already done that, I've not bothered...
一旦您拥有正确的格式,您就可以使用JodaTime。您可以String
使用 JodaTime 直接解析该值,但既然您已经这样做了,我就没有打扰...
Date date = ...;
DateTime dt = new DateTime(date);
dt = dt.plusDays(102);
date = dt.toDate();
回答by Deep Mukherjee
import java.util.*;
class ISCprac2009q01{
public static void main(String args[])
throws InputMismatchException{
Scanner scan=new Scanner(System.in);
System.out.println("ENTER DAY NUMER(>=1 AND
<=366) : ");
int day_number=scan.nextInt();
System.out.println("ENTER YEAR(4 DIGIT) : ");
int year=scan.nextInt();
System.out.println("ENTER DATE AFTER(N)(>=1 AND
<=100) : ");
int n=scan.nextInt();
if(day_number<1 || day_number>366)
System.out.println("INVALID DAY NUMBER.");
else if(year<1000 || year >9999)
System.out.println("INVALID YEAR");
else if(n<1 || n>100)
System.out.println("INVALID DATE AFTER VALUE.");
else{
//INITIALIZE MONTH NAMES AND NUMBER OF DAYS IN EACH MONTH
String month_names[]={"JANUARY", "FEBRUARY","MARCH",
"APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER",
"OCTOBER","NOVERMBER","DECEMBER"};
int month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int i, day, month,day_after;
String suffix;
//IF IT IS A LEAP YEAR FEBRURAY SHOULD HAVE 29 DAYS
if(year%400==0 || (year%100!=0 && year%4==0))
month_days[1]=29;
i=0;
//FIND THE DATE CORRESPONDING TO THE DAY NUMBER
day=day_number;
while(day>month_days[i])
{
day-=month_days[i];
i++;
}
month=i;
//ADD SUFFIX AS PER THE DAY
if(day%10==1 && day/10!=1)
suffix="ST";
else if(day%10==2 && day/10!=1)
suffix="ND";
else if(day%10==3 && day/10!=1)
suffix="RD";
else
suffix="TH";
System.out.println("OUTPUT:");
//FIRST PART OF THE OUTPUT
System.out.println(day+suffix+" "+
month_names[month]+" "+year);
//TO CALCULATE DATE AFTER N DAYS
day_after=day_number+n;
i=0;
while(day_after>month_days[i])
{
day_after-=month_days[i];
i++;
if(i==12){
i=0;
year++;
if(year%400==0 || (year%100!=0 && year%4==0))
month_days[1]=29;
}
}
day=day_after;
month=i;
//ADD SUFFIX AS PER THE DAY
if(day%10==1 && day/10!=1)
suffix="ST";
else if(day%10==2 && day/10!=1)
suffix="ND";
else if(day%10==3 && day/10!=1)
suffix="RD";
else
suffix="TH";
//SECOND PART OF THE OUTPUT
System.out.println(day+suffix+" "+
month_names[month]+" "+year);
}
}
}