要使用日期值操作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();
如果你需要插入值到數據庫中,你當然會使用PreparedStatement
,所以你需要一個Timestamp
代替:
// From Date
Timestamp futureTimestamp = new Timestamp(futureDate.getTime());
// Directly from Calendar
Timestamp futureTimestamp = new Timestamp(cal.getTimeInMillis());
怎麼樣參照這個視頻教程 的https: //www.youtube.com/watch?v=q1-ttUrRhl0 – AVI