我设置了时间选择器并且它工作正常,但问题是我想设置为 5:00 AM
,我得到输出24小时格式但我想设置12小时格式。
这里的代码: -
package com.example.sachin.timepicker;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
public class MainActivity extends Activity {
boolean isFromClicked = false;
TextView tvDisplayTime, tvDisplayTime1;
EditText btnChangeTime, btnChangeTime1, addtaskname, addtaskdetail;
Button addtaskbtn;
private int hour;
private int minute;
String addtask, adddetail, addstart, addend;
static final int TIME_DIALOG_ID = 999;
private static String url_addevent = "http://10.0.2.2/portal/index.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnChangeTime = (EditText) findViewById(R.id.btnChangeTime);
btnChangeTime1 = (EditText) findViewById(R.id.btnChangeTime1);
addtaskname = (EditText) findViewById(R.id.addtaskname);
addtaskdetail = (EditText) findViewById(R.id.addtaskdetail);
addtaskbtn = (Button) findViewById(R.id.addtaskbtn);
addtaskbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
addtask = addtaskname.getText().toString();
addstart = btnChangeTime.getText().toString();
addend = btnChangeTime1.getText().toString();
adddetail = addtaskdetail.getText().toString();
if (addtask.equals(null) || addtask.equals("")) {
Toast.makeText(getApplicationContext(), "Please Add Name !", Toast.LENGTH_LONG).show();
} else if (addstart.equals(null) || addstart.equals("")) {
Toast.makeText(getApplicationContext(), "Please Add Duty!", Toast.LENGTH_LONG).show();
} else if (addend.equals(null) || addend.equals("")) {
Toast.makeText(getApplicationContext(), "Please Add Number!", Toast.LENGTH_LONG).show();
} else if (adddetail.equals(null) || adddetail.equals("")) {
Toast.makeText(getApplicationContext(), "Please Add Number!", Toast.LENGTH_LONG).show();
} else {
securityUser();
Toast.makeText(getApplicationContext(), "Event Added Successfully. !", Toast.LENGTH_LONG).show();
}
}
});
setCurrentTimeOnView();
addListenerOnButton();
}
private void securityUser() {
daily_task(addtask, addstart, addend, adddetail);
}
private void daily_task(String addtask, String addstart, String addend, String adddetail) {
class eventUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
//@Override
protected void onPreExecute() {
// Create a progressdialog
super.onPreExecute();
// loading = ProgressDialog.show(getApplicationContext(), "Please Wait...!!!", null, true, true);
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> param = new HashMap<String, String>();
param.put("dt_tname", params[0]);
param.put("dt_stime", params[1]);
param.put("dt_etime", params[2]);
param.put("dt_detail", params[3]);
String result = ruc.sendPostRequest(url_addevent, param);
Log.d("Result", result);
Log.d("Data", param.toString());
return result;
}
//@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// loading.dismiss();
Toast.makeText(getApplicationContext(), "Security Added Successfully...!!!", Toast.LENGTH_LONG).show();
/* Intent i1 = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(i1);*/
}
}
eventUser regi = new eventUser();
regi.execute(addtask, addstart, addend, adddetail);
}
// display current time
public void setCurrentTimeOnView() {
btnChangeTime = (EditText) findViewById(R.id.btnChangeTime);
btnChangeTime1 = (EditText) findViewById(R.id.btnChangeTime1);
// timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
// set current time into textview
btnChangeTime.setText(new StringBuilder().append(pad(hour)).append(":")
.append(pad(minute)));
btnChangeTime1.setText(new StringBuilder().append(pad(hour)).append(":")
.append(pad(minute)));
// set current time into timepicker
//timePicker1.setCurrentHour(hour);
//timePicker1.setCurrentMinute(minute);
}
public void addListenerOnButton() {
btnChangeTime = (EditText) findViewById(R.id.btnChangeTime);
btnChangeTime1 = (EditText) findViewById(R.id.btnChangeTime1);
btnChangeTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
isFromClicked = false;
}
});
btnChangeTime1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
isFromClicked = true;
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
}
return null;
}
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
// set current time into textview
if ( isFromClicked )
btnChangeTime1.setText(new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
else
btnChangeTime.setText(new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
// set current time into timepicker
// timePicker1.setCurrentHour(hour);
// timePicker1.setCurrentMinute(minute);
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
上面的代码是我的完整代码,请帮我使用相同的代码
答案 0 :(得分:1)
您可以使用以下转换
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
int hour = hourOfDay % 12;
if (hour == 0) hour = 12;
String _AM_PM = (hourOfDay > 12) ? "PM" : "AM";
System.out.println(String.format(Locale.getDefault(), "%02d:%02d %s", hour, minute, _AM_PM));
}
从 TimePickerDialog 中选择时间时会调用此覆盖函数。
完整答案
package com.example.sachin.timepicker;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
public class MainActivity extends Activity {
boolean isFromClicked = false;
TextView tvDisplayTime, tvDisplayTime1;
EditText btnChangeTime, btnChangeTime1, addtaskname, addtaskdetail;
Button addtaskbtn;
private int hour;
private int minute;
String addtask, adddetail, addstart, addend;
static final int TIME_DIALOG_ID = 999;
private static String url_addevent = "http://10.0.2.2/portal/index.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnChangeTime = (EditText) findViewById(R.id.btnChangeTime);
btnChangeTime1 = (EditText) findViewById(R.id.btnChangeTime1);
addtaskname = (EditText) findViewById(R.id.addtaskname);
addtaskdetail = (EditText) findViewById(R.id.addtaskdetail);
addtaskbtn = (Button) findViewById(R.id.addtaskbtn);
addtaskbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
addtask = addtaskname.getText().toString();
addstart = btnChangeTime.getText().toString();
addend = btnChangeTime1.getText().toString();
adddetail = addtaskdetail.getText().toString();
if (addtask.equals(null) || addtask.equals("")) {
Toast.makeText(getApplicationContext(), "Please Add Name !", Toast.LENGTH_LONG).show();
} else if (addstart.equals(null) || addstart.equals("")) {
Toast.makeText(getApplicationContext(), "Please Add Duty!", Toast.LENGTH_LONG).show();
} else if (addend.equals(null) || addend.equals("")) {
Toast.makeText(getApplicationContext(), "Please Add Number!", Toast.LENGTH_LONG).show();
} else if (adddetail.equals(null) || adddetail.equals("")) {
Toast.makeText(getApplicationContext(), "Please Add Number!", Toast.LENGTH_LONG).show();
} else {
securityUser();
Toast.makeText(getApplicationContext(), "Event Added Successfully. !", Toast.LENGTH_LONG).show();
}
}
});
setCurrentTimeOnView();
addListenerOnButton();
}
private void securityUser() {
daily_task(addtask, addstart, addend, adddetail);
}
private void daily_task(String addtask, String addstart, String addend, String adddetail) {
class eventUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
//@Override
protected void onPreExecute() {
// Create a progressdialog
super.onPreExecute();
// loading = ProgressDialog.show(getApplicationContext(), "Please Wait...!!!", null, true, true);
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> param = new HashMap<String, String>();
param.put("dt_tname", params[0]);
param.put("dt_stime", params[1]);
param.put("dt_etime", params[2]);
param.put("dt_detail", params[3]);
String result = ruc.sendPostRequest(url_addevent, param);
Log.d("Result", result);
Log.d("Data", param.toString());
return result;
}
//@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// loading.dismiss();
Toast.makeText(getApplicationContext(), "Security Added Successfully...!!!", Toast.LENGTH_LONG).show();
/* Intent i1 = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(i1);*/
}
}
eventUser regi = new eventUser();
regi.execute(addtask, addstart, addend, adddetail);
}
// display current time
public void setCurrentTimeOnView() {
btnChangeTime = (EditText) findViewById(R.id.btnChangeTime);
btnChangeTime1 = (EditText) findViewById(R.id.btnChangeTime1);
// timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
// set current time into textview
btnChangeTime.setText(getTimeText(hour, minute));
btnChangeTime1.setText(getTimeText(hour, minute));
// set current time into timepicker
//timePicker1.setCurrentHour(hour);
//timePicker1.setCurrentMinute(minute);
}
public void addListenerOnButton() {
btnChangeTime = (EditText) findViewById(R.id.btnChangeTime);
btnChangeTime1 = (EditText) findViewById(R.id.btnChangeTime1);
btnChangeTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showTimePickerDialog();
isFromClicked = false;
}
});
btnChangeTime1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showTimePickerDialog();
isFromClicked = true;
}
});
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
// set current time into textview
if (isFromClicked)
btnChangeTime1.setText(getTimeText(hour, minute));
else
btnChangeTime.setText(getTimeText(hour, minute));
// set current time into timepicker
// timePicker1.setCurrentHour(hour);
// timePicker1.setCurrentMinute(minute);
}
};
private void showTimePickerDialog() {
new TimePickerDialog(this, timePickerListener, hour, minute, true).show();
}
private String getTimeText(int hourOfDay, int minute) {
int hour = hourOfDay % 12;
if (hour == 0) hour = 12;
String _AM_PM = (hourOfDay > 12) ? "PM" : "AM";
return String.format(Locale.getDefault(), "%02d:%02d %s", hour, minute, _AM_PM);
}
}
答案 1 :(得分:0)
您可以使用
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss");
formattedDate = df.format(c.getTime());
System.out.println("Date : "+formattedDate);
答案 2 :(得分:0)
您可以使用SimpleDateFormat类在12小时内获取时间。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.US);//First argument will be the format
String timeFormat = sdf.format(/*Pass your time here*/);
所以在你的onTimeSet()方法中只需添加上面的行。
像,
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.US);//First argument will be the format
String timeFormat = sdf.format(/*Pass your time here*/);
btnChangeTime1.setText(timeFormat);
}