[英]Adding 30 days on the value from JDateChooser using Java netbeans
hi all I am working on a form using JDateChooser
I want to get the value of date inputted by the user. 大家好,我正在使用
JDateChooser
处理表单,我想获取用户输入的日期值。 Is there any ways that after storing the value of date in a variable can I add 30 days from the inputted date? 有什么方法可以将date的值存储在变量中,然后可以从输入的日期起增加30天? This is my code in passing the date to a string variable:
这是将日期传递给字符串变量的代码:
String dates =((JTextField)date.getDateEditor().getUiComponent()).getText();
my problem is how can I pass the date into a variable where i can be able to add an additional 30 days on it? 我的问题是如何将日期传递到变量中,在该变量上可以再添加30天? please help me really need this for my project .
请帮助我的项目确实需要这个。
To manipulate a String
with a date value, you first need to convert to a Date
using SimpleDateFormat
, then you can perform manipulation using a Calendar
. 要使用日期值操作
String
,首先需要使用SimpleDateFormat
转换为Date
,然后可以使用Calendar
进行操作。
SimpleDateFormat datefmt = new SimpleDateFormat("MM/dd/yyyy"); // Or format you're using
Date date = datefmt.parse(dates);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 30); // Add 30 days
Date futureDate = cal.getTime();
If you need value for insertion into a database, you will of course use PreparedStatement
, so you'll need a Timestamp
instead: 如果您需要插入数据库的值,那么您当然会使用
PreparedStatement
,因此需要一个Timestamp
:
// From Date
Timestamp futureTimestamp = new Timestamp(futureDate.getTime());
// Directly from Calendar
Timestamp futureTimestamp = new Timestamp(cal.getTimeInMillis());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.