2013-05-13 37 views

回答

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

documentation

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如果有什么可怕的情况发生。