如何在适当的时间格式中添加额外的零

时间:2016-06-01 10:03:00

标签: android calendar timepicker timepickerdialog

我正在使用时间选择器来选择时间,然后以24小时格式以吐司形式显示它。我正在像“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();


    }
}                                      

8 个答案:

答案 0 :(得分:7)

int只是一个整数。整数没有前导零。将它们转换为String个对象时,它们只能以前导零显示。一种方法是这样的:

int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

String curTime = String.format("%02d:%02d", hour, minute);

答案 1 :(得分:4)

首先需要创建一个检查输入的函数,并根据条件将其转换为String。

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);

答案 2 :(得分:1)

你可以写这个

  Toast myToast = Toast.makeText(getContext(), hourOfDay <10? "0"+hourOfDay :hourOfDay  + ":" + minute<10:"0"+minute:minute, Toast.LENGTH_SHORT);

答案 3 :(得分:1)

您只需使用String.format,如下所示:

String format = "%1$02d"; // two digits
Toast.makeText(getContext(), String.format(format, hourOfDay)+ ":" + String.format(format, minute, Toast.LENGTH_SHORT).show();

答案 4 :(得分:1)

试试这个

private String fillText(int i){
    return i>9?i+"":"0"+i;
}

...

 Toast myToast = Toast.makeText(getContext(), fillText(hourOfDay) + ":" + filllText(minute), Toast.LENGTH_SHORT);

答案 5 :(得分:1)

您可以使用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

答案 6 :(得分:1)

在strings.xml中添加此字符串ressource

<string name="time_remaining">%1$02d:%2$02d</string>

在onTimeSet函数中,您可以按如下方式使用它:

String time = getActivity().getResources().getString(R.string.time_remaining, hourOfDay, minute);
Toast myToast = Toast.makeText(getContext(), time, Toast.LENGTH_SHORT);
myToast.show();

答案 7 :(得分:0)

Kotlin v.1.4.x

这是 Int 的扩展:

fun Int.to2DString():String{
    return String.format("%02d",this)
}

用法:

val formattedMinute = timePicker.minute.to2DString()