1

我正在创建一个应用程序,我希望客户端从 DatePicker 中选择日期和时间,并将它们以日期时间格式存储到 SQL 服务器。有任何想法吗?因为我用过SimpleDateFormat但是没用。

4

3 回答 3

1

实际上格式化日期和时间在 sql 中很重要。

首先,您必须考虑从日期选择器获取的日期格式(通常是我们向用户显示的格式):

常量 ::

public static final String DATE_FORMAT = "MM/dd/yyyy";
public static final String SQL_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String SQL_DATE_FORMAT = "yyyy-MM-dd";

SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.US);
SimpleDateFormat rdf = new SimpleDateFormat(SQL_DATE_FORMAT,
                Locale.US);
SimpleDateFormat tdf = new SimpleDateFormat(SQL_DATETIME_FORMAT,
                Locale.US);

日期选择器 ::

public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        Bundle data = this.getArguments();
        if (data.getBoolean("isSelected")) {
            year = data.getInt("year") + 1900;
            month = data.getInt("month");
            day = data.getInt("day");
        }

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.US);
        @SuppressWarnings("deprecation")
        String formatedDate = sdf.format(new Date(year - 1900, month, day));
        ((BaseActivity) getActivity()).mValuePickListener.onPickedValue(
                formatedDate, DIALOG_DATE_PICKER);
    }
}

现在,将此格式转换为 mysql 接受的格式以存储日期和时间:

Date selectedDate = new Date();
if (!date.getText().toString().trim().equalsIgnoreCase("")) {
  selectedDate =    sdf.parse(date.getText().toString());
}
String sqlDate = rdf.format(selectedDate);

PS ::您还可以使用时间戳(长)在数据库中存储时间

于 2013-05-13T12:50:56.137 回答
0

看看广告包装类,但在 DAO 级别,java 端应该是java.sql.Date

文档

8.3.12   DATE, TIME, and TIMESTAMP

There are three JDBC types relating to time:

The JDBC DATE type represents a date consisting of day, month, and year. The corresponding SQL DATE type is defined in SQL-92, but it is implemented by only a subset of the major databases. Some databases offer alternative SQL types that support similar semantics.
The JDBC TIME type represents a time consisting of hours, minutes, and seconds. The corresponding SQL TIME type is defined in SQL-92, but it is implemented by only a subset of the major databases. As with DATE, some databases offer alternative SQL types that support similar semantics.
The JDBC TIMESTAMP type represents DATE plus TIME plus a nanosecond field. The corresponding SQL TIMESTAMP type is defined in SQL-92, but it is implemented by only a very small number of databases.
Because the standard Java class java.util.Date does not match any of these three JDBC date/time types exactly (it includes both DATE and TIME information but has no nanoseconds), JDBC defines three subclasses of java.util.Date to correspond to the SQL types. They are:

java.sql.Date for SQL DATE information. The hour, minute, second, and millisecond fields of the java.util.Date base class should be set to zero. If the number of milliseconds supplied to the java.sql.Date constructor is negative, the driver will compute the date as the number of milliseconds before January 1, 1970. Otherwise, the date is computed as the specified number of milliseconds after January 1, 1970.

java.sql.Time for SQL TIME information. The year, month, and day fields of the java.util.Date base class are set to 1970, January, and 1. This is the "zero" date in the Java epoch.
java.sql.Timestamp for SQL TIMESTAMP information. This class extends java.util.Date by adding a nanoseconds field.
All three of the JDBC time-related classes are subclasses of java.util.Date, and as such, they can be used where a java.util.Date is expected. For example, internationalization methods take a java.util.Date object as an argument, so they can be passed instances of any of the JDBC time-related classes.

A JDBC Timestamp object has its parent's date and time components and also a separate nanoseconds component. If a java.sql.Timestamp object is used where a java.util.Date object is expected, the nanoseconds component is lost. However, since a java.util.Date object is stored with a precision of one millisecond, it is possible to maintain this degree of precision when converting a java.sql.Timestamp object to a java.util.Date object. This is done by converting the nanoseconds in the nanoseconds component to whole milliseconds (by dividing the number of nanoseconds by 1,000,000) and then adding the result to the java.util.Date object. Up to 999,999 nanoseconds may be lost in this conversion, but the resulting java.util.Date object will be accurate to within one millisecond.

The following code fragment is an example of converting a java.sql.Timestamp object to a java.util.Date object that is accurate to within one millisecond.

Timestamp t = new Timestamp(98724573287540L);
java.util.Date d;
d = new java.util.Date(t.getTime() + (t.getNanos() / 1000000));
New methods in the JDBC 2.0 core API make it possible for the driver to take a specified time zone into account when calculating a date, time, or timestamp. The time zone information is included in a java.util.Calendar object that is passed to new versions of the methods for getting and setting Date, Time, and Timestamp values. When no time zone is specified, the driver uses the time zone of the virtual machine running the application when it calculates a date, time, or timestamp.
于 2013-05-13T12:42:23.200 回答
0
public void onDateSet(DatePicker view, int year, int month, int day) throws SQLException {
    java.sql.Date myDate = (java.sql.Date)new java.util.Date(year, month, day);
    PreparedStatement mySqlInsertStatement = connection.prepareStatement(sqlStatement, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    mySqlInsertStatement.setDate(1, myDate);
    if (mySqlInsertStatement.executeUpdate() != 1) { // we have a problem
    }
}

如果发生可怕的事情, executeUpdate会返回the row count for INSERT并抛出SQLException 。

于 2013-05-13T13:04:04.230 回答