Trying to insert Current date from android to mysql via Retrofit. The date is showing in the mysql database as 0000-00-00. I've read over multiple threads on SO on this topic and tried to apply the answers but so far I have not been successful. Please see the below code snippets for context.
PHP CODE:
<?php
include('conn.php');
$userId = $_POST['userId'];
$moodBefore = $_POST['moodBefore'];
$automaticThought = $_POST['automaticThought'];
$distortions = $_POST['distortions'];
$challengeThought = $_POST['challengeThought'];
$alternativeThought = $_POST['alternativeThought'];
$moodAfter = $_POST['moodAfter'];
$posted = $_POST['posted'];
$insert = "INSERT INTO Cbt ( userId, moodBefore, automaticThought, distortions, challengeThought, alternativeThought, moodAfter, posted) VALUES
('$userId','$moodBefore', '$automaticThought', '$distortions', '$challengeThought', '$alternativeThought', '$moodAfter', '$posted')";
$resultinsert = $conn -> query($insert);
if(!$resultinsert){
echo $conn->error;
}
?>
RETROFIT API CODE:
public interface Api {
@FormUrlEncoded
@POST("insert.php")
Call<ResponseBody> insertLog(
@Field("userId") int userId,
@Field("moodBefore") int moodBefore,
@Field("automaticThought") String automaticThought,
@Field("distortions") int distortions,
@Field("challengeThought") String challengeThought,
@Field("alternativeThought") String alternativeThought,
@Field("moodAfter") int moodAfter,
@Field("posted") String date
);
JAVA CODE:
String posted = new SimpleDateFormat("ddMMyyyy").format(new Date());
case R.id.nextBtnMoodPage:
if (hasSelected) {
Call<ResponseBody> call = RetrofitClient
.getInstance()
.getApi()
.insertLog(userId, moodBefore, automaticThoughtString, distortions, challengeThoughtString, alternativeThoughtString, moodAfter, posted);
call.enqueue(new Callback<ResponseBody>() {.....and so on
I would like the date to insert succesfully and not show 0000-00-00 If I could also make the date show as DD-MM-YYYY that would be even better.
This worked in the Java code, all other parts of the code stay the same
private Date currentDate() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 0);
return cal.getTime();
}
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String posted = dateFormat.format(currentDate());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.