gpt4 book ai didi

java - 使用android将日期时间导入mysql

转载 作者:太空狗 更新时间:2023-10-29 14:39:59 24 4
gpt4 key购买 nike

如何在 android 中获取当前日期和时间并插入到 mysql 数据库中...我正在使用 EditText 并试图在其中获取当前日期和时间...但我无法插入它...所以任何人都可以分享你的想法如何在 edittext 中获取当前日期和时间并插入到 mysql 数据库中......

Calendar calendar = Calendar.getInstance();
String currentdate = DateFormat.getDateInstance().format(calendar.getTime());

tname = (EditText) findViewById(R.id.name);
tname.setText(currentdate);

正在尝试获取日期....

private void registerUser() {
String date =tname.getText().toString().trim().toLowerCase();
register(date);
}

完整代码

public class MainActivity extends AppCompatActivity {

EditText tname;
Button button;
private static final String REGISTER_URL="http://192.168.0.106/test/checkin.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Calendar calendar = Calendar.getInstance();
String currentdate = DateFormat.getDateInstance().format(calendar.getTime());

tname = (EditText) findViewById(R.id.name);
tname.setText(currentdate);
button = (Button) findViewById(R.id.btn);

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
registerUser();
}
});
}

private void registerUser() {
String date =tname.getText().toString().trim().toLowerCase();
register(date);
}

private void register(String date) {
String urlSuffix = "?date=" + date;
class RegisterUser extends AsyncTask<String, Void, String> {

ProgressDialog loading;

@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this, "Please Wait", null, true, true);
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),"Registered", Toast.LENGTH_SHORT).show();
}

@Override
protected String doInBackground(String... params) {
String s = params[0];
BufferedReader bufferReader=null;
try {
URL url=new URL(REGISTER_URL+s);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
bufferReader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
result=bufferReader.readLine();
return result;

}catch (Exception e){
return null;
}
}

}
RegisterUser ur=new RegisterUser();
ur.execute(urlSuffix);
}
}

最佳答案

试试这段代码

// Parse the date
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm");
Date selectedDate = sdf.parse("04-11-2018 01:10");

// then create the MySQL datetime string
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(selectedDate);

关于java - 使用android将日期时间导入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49769031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com