简体   繁体   English

Java:日期操作

[英]Java: Date manipulation

I'm trying to modify the date using SimpleDateFormat and Calendar object.我正在尝试使用 SimpleDateFormat 和 Calendar 对象修改日期。 Then writes modified date into my database.然后将修改日期写入我的数据库。 The problem is, my method won't compile.问题是,我的方法无法编译。 It says: Incompatible types String cannot be converted to Date.它说:不兼容的类型字符串不能转换为日期。 What did i miss?我错过了什么? This is my method这是我的方法

public Date expiredItem() {

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar expiredDate = Calendar.getInstance();
    expiredDate.add(Calendar.DATE, Integer.parseInt(timeFld.getText()));
    //timeFld is JTextField.getText() casted into int

    // The confusing thing is, below Dialog works as i expected but not on my
    // method return.
    JOptionPane.showMessageDialog(null, "Expiration Date: " +       
    dateFormat.format(expiredDate.getTime()));
    return  dateFormat.format(expiredDate.getTime()); //Erroneous code
}

tl;dr tl;博士

myPreparedStatement.setObject( 
    … ,
    LocalDate.now( ZoneId.of( "America/Montreal" ) )
             .plusDays( … )
)

Details细节

Question is not clear, but it sounds like you are trying to get the current date, add some number of days (input as text) to determine an “expiration” date, and display that as text.问题不清楚,但听起来您正在尝试获取当前日期,添加一些天数(作为文本输入)以确定“到期”日期,并将其显示为文本。

You are using confusing troublesome old date-time classes that are now legacy, supplanted by the java.time classes.您正在使用令人困惑、麻烦的旧日期时间类,这些类现在是遗留的,被 java.time 类取代。

Getting the current date requires a time zone.获取当前日期需要时区。 For any given moment the date varies around the globe by zone.对于任何给定时刻,日期因地区而异。

ZoneId z = ZoneId.of( "America/Montreal" );

You can use the JVM's current default time zone.您可以使用 JVM 的当前默认时区。 But know that the default can be changed at any moment by any code in any thread of any app within the JVM.但是要知道,JVM 中任何应用程序的任何线程中的任何代码都可以随时更改默认值。 So if crucial, ask the user for desired/expected zone.因此,如果至关重要,请向用户询问所需/预期区域。

ZoneId z = ZoneId.systemDefault() ;

To represent a date-only value without a time-of-day and without a time zone, use LocalDate .要表示没有时间和时区的仅日期值,请使用LocalDate

LocalDate today = LocalDate.now( z );

Parse the number-of-days text into a long .将天数文本解析为long .

String input = timeFld.getText() ;
long days = Long.parseLong( input );

Add days to your today date.将天数添加到您的today日期。

LocalDate expiration = today.plusDays( days ) ;

To generate a String representing that value in standard ISO 8601 format YYYY-MM-DD, simply call toString .要以标准 ISO 8601 格式 YYYY-MM-DD 生成表示该值的字符串,只需调用toString

String output = expiration.toString();

2017-01-23 2017-01-23

If your JDBC driver complies with JDBC 4.2 or later, then you can exchange this LocalDate with your database via PreparedStatement::setObject and ResultSet::getObject methods.如果您的 JDBC 驱动程序符合 JDBC 4.2 或更高版本,那么您可以通过PreparedStatement::setObjectResultSet::getObject方法将此LocalDate与您的数据库交换。

For older drivers, convert to/from the java.sql.Date class using new conversion methods added to the old class.对于较旧的驱动程序,使用添加到旧类的新转换方法在java.sql.Date类之间进行转换。

java.sql.Date sqlDate = java.sql.Date.valueOf( expiration );

LocalDate expiration = sqlDate.toLocalDate();

Tip: Separate the code doing business logic (calculating the expiration) from the user-interface work of gathering input and displaying result.提示:将执行业务逻辑(计算到期时间)的代码与收集输入和显示结果的用户界面工作分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM