如何为正确的时间格式及时添加一个额外的零
How to add an Extra zero in time for proper time format
我正在使用时间选择器来选择时间,然后以 24 小时格式在 toast 中显示它。我得到像“2:6”这样的吐司输出,我想以适当的时间格式得到它,比如“02:06”。我该怎么做....
public void ShowTimePicker(View view) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast myToast = Toast.makeText(getContext(), hourOfDay + ":" + minute, Toast.LENGTH_SHORT);
myToast.show();
}
}
你可以写这个
Toast myToast = Toast.makeText(getContext(), hourOfDay <10? "0"+hourOfDay :hourOfDay + ":" + minute<10:"0"+minute:minute, Toast.LENGTH_SHORT);
首先需要创建一个函数来检查您的输入并根据条件将其转换为字符串。
public String convertDate(int input) {
if (input >= 10) {
return String.valueOf(input);
} else {
return "0" + String.valueOf(input);
}
}
那你可以这样调用
txtTime.setText(convertDate(hourOfDay) + ":" + convertDate(minute));
你的吐司会是这样的
Toast myToast = Toast.makeText(getContext(), convertDate(hourOfDay) + ":" + convertDate(minute), Toast.LENGTH_SHORT);
您只需要使用 String.format 如下:
String format = "%1d"; // two digits
Toast.makeText(getContext(), String.format(format, hourOfDay)+ ":" + String.format(format, minute, Toast.LENGTH_SHORT).show();
试试这个
private String fillText(int i){
return i>9?i+"":"0"+i;
}
...
Toast myToast = Toast.makeText(getContext(), fillText(hourOfDay) + ":" + filllText(minute), Toast.LENGTH_SHORT);
您可以使用 SimpleDateFormat 进行日期和时间格式化:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
String string = sdf.format(callendar);
您可以在此处找到有关时间模式的信息:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
int
只是一个整数。整数没有前导零。当您将它们转换为 String
对象时,它们只能以前导零显示。一种方法是这样的:
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
String curTime = String.format("%02d:%02d", hour, minute);
将此字符串资源添加到您的 strings.xml
<string name="time_remaining">%1d:%2d</string>
在您的 onTimeSet 函数中,您可以按如下方式使用它:
String time = getActivity().getResources().getString(R.string.time_remaining, hourOfDay, minute);
Toast myToast = Toast.makeText(getContext(), time, Toast.LENGTH_SHORT);
myToast.show();
Kotlin v.1.4.x
这是 Int 的扩展:
fun Int.to2DString():String{
return String.format("%02d",this)
}
用法:
val formattedMinute = timePicker.minute.to2DString()
我正在使用时间选择器来选择时间,然后以 24 小时格式在 toast 中显示它。我得到像“2:6”这样的吐司输出,我想以适当的时间格式得到它,比如“02:06”。我该怎么做....
public void ShowTimePicker(View view) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast myToast = Toast.makeText(getContext(), hourOfDay + ":" + minute, Toast.LENGTH_SHORT);
myToast.show();
}
}
你可以写这个
Toast myToast = Toast.makeText(getContext(), hourOfDay <10? "0"+hourOfDay :hourOfDay + ":" + minute<10:"0"+minute:minute, Toast.LENGTH_SHORT);
首先需要创建一个函数来检查您的输入并根据条件将其转换为字符串。
public String convertDate(int input) {
if (input >= 10) {
return String.valueOf(input);
} else {
return "0" + String.valueOf(input);
}
}
那你可以这样调用
txtTime.setText(convertDate(hourOfDay) + ":" + convertDate(minute));
你的吐司会是这样的
Toast myToast = Toast.makeText(getContext(), convertDate(hourOfDay) + ":" + convertDate(minute), Toast.LENGTH_SHORT);
您只需要使用 String.format 如下:
String format = "%1d"; // two digits
Toast.makeText(getContext(), String.format(format, hourOfDay)+ ":" + String.format(format, minute, Toast.LENGTH_SHORT).show();
试试这个
private String fillText(int i){
return i>9?i+"":"0"+i;
}
...
Toast myToast = Toast.makeText(getContext(), fillText(hourOfDay) + ":" + filllText(minute), Toast.LENGTH_SHORT);
您可以使用 SimpleDateFormat 进行日期和时间格式化:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
String string = sdf.format(callendar);
您可以在此处找到有关时间模式的信息:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
int
只是一个整数。整数没有前导零。当您将它们转换为 String
对象时,它们只能以前导零显示。一种方法是这样的:
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
String curTime = String.format("%02d:%02d", hour, minute);
将此字符串资源添加到您的 strings.xml
<string name="time_remaining">%1d:%2d</string>
在您的 onTimeSet 函数中,您可以按如下方式使用它:
String time = getActivity().getResources().getString(R.string.time_remaining, hourOfDay, minute);
Toast myToast = Toast.makeText(getContext(), time, Toast.LENGTH_SHORT);
myToast.show();
Kotlin v.1.4.x
这是 Int 的扩展:
fun Int.to2DString():String{
return String.format("%02d",this)
}
用法:
val formattedMinute = timePicker.minute.to2DString()