我正在创建一个应用程序,我希望客户端从DatePicker中选择日期和时间并将它们以datetime格式存储到SQL服务器。有任何想法吗?因为我使用了SimpleDateFormat
,但它没有奏效。从Datepicker和时间timepicker对话框存储日期和时间到MySQL
1
A
回答
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 ::您也可以使用时间戳(长),用于存储时间在数据库
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.
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如果有什么可怕的情况发生。
相关问题
- 1. PHP和MySQL:日期+时间存储
- 2. Datepicker和timepicker从一个对话框内
- 3. DatePicker-TimePicker到一个MySQL时间戳
- 4. 保存日期和时间从VB.Net到MySQL数据库日期?
- 5. 从存储日期时间
- 6. 从datepicker对话框设置任何日期和时间的闹钟
- 7. 如何转换日期和时间存储在MySQL日期时间
- 8. PHP MYSQL日期,时间,日期时间和时间戳
- 9. 从日期时间格式中分离日期和时间mysql
- 10. 在猪存储日期和时间
- 11. MongoDB和存储日期时间字段
- 12. 在tarantool中存储日期和时间
- 13. mysql和python日期时间
- 14. PHP日期时间和mysql
- 15. MySQL和日期时间
- 16. 从时区和日期和时间创建时间对象?
- 17. 我怎么可以添加datepicker和timepicker列jtable的项目日期,从时间和时间?
- 18. Mysql存储和检索时间和日期
- 19. 在MySQL中存储日期和时间(不含分钟和秒)
- 20. 在会话中存储日期时间
- 21. jQuery DatePicker设置时间和日期
- 22. 如何从日期时间戳得到时间和日期
- 23. 从VB.Net中保存日期和时间mysql数据库日期?
- 24. 从时间和日期删除时间
- 25. 保存日期和时间
- 26. mysql日期和日期时间问题
- 27. 日期和时间到Python的时间
- 28. 插入日期和时间到Mysql
- 29. Mysql不在时间和日期之间
- 30. 如何只存储时间;不是日期和时间?