android - sql語言 - 每次向伺服器(安卓)發送報表時,如何獲取當前日期?

用此方法向伺服器發送報表


public void sendReport(Activity activity, final Report report) {



 Interface communicator = RestAdapterHelper.getInstance().createAdapter(activity);



 Calendar c = Calendar.getInstance();


 c.getTime();


 SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");


 String formattedDate = df.format(c.getTime());


 report.setDateTime(formattedDate);


 report.getDateTime().equals(formattedDate);



 Call<ServerResponse> caller = communicator.sendReport(


 Functions.getInstance().getReportUrl(activity),


 report.getId(),


 report.getDeviceId(),


 report.getCampaignId(),


 report.getBranchId(),


 report.getDateTime()


 );



}

用此方法添加數據


public static void addReport(Report report) {



 Calendar c = Calendar.getInstance();


 c.getTime();


 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


 String formattedDate = df.format(c.getTime());


 report.setDateTime(formattedDate);


 report.getDateTime().equals(formattedDate);



 String query ="INSERT INTO reports (id,device_id,campaign_id,branch_id,,datetime,is_reported) VALUES(null,'"


 + report.getDeviceId() +"','"


 + report.getCampaignId() +"','"


 + report.getBranchId() +"','"


 + report.getDateTime() +"','0');";



 database.execSQL(query);



 resultSet = database.rawQuery("SELECT * from reports"/* where is_reported = 0"*/, null);


 while (resultSet.moveToNext()) {


 //Get the last inserted ID


 latestReportId = resultSet.getString(0);


 }


 report.setId(latestReportId);


 new Communicator().sendReport(activity, report);


}



這是我創建資料庫的方法


 private void setUpDatabase() {



 database = openOrCreateDatabase("user_reports", MODE_PRIVATE, null);


 String query ="CREATE TABLE IF NOT EXISTS reports(id integer primary key autoincrement,device_id VARCHAR,campaign_id INTEGER, branch_id INTEGER, datetime TIMESTAMP, is_reported INTEGER);";


 database.execSQL(query);


}



时间:

你可以像下面這樣把formattedDate傳遞給伺服器和資料庫,


 public static void getCurrentTimeUsingDate() {


 Date date = new Date();


 String strDateFormat ="dd-MM-yyyy hh:mm:ss a";


 DateFormat dateFormat = new SimpleDateFormat(strDateFormat);


 String formattedDate= dateFormat.format(date); 


 Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();


 }



...