我希望以早上05:05的格式显示时间,并存储在sqlite数据库中。请如何将其存储在db中以及如何更改时间格式。
这是我的代码:
edittime.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View
final Calendar mcurrentTime = Calendar.getInstance();
final int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(CreatNotes.this, new TimePickerDialog.
@Override
public void onTimeSet(TimePicker timePicker, int hours, int minutes) {
String AM_PM = " AM";
String mm_precede = "";
if (hours >= 12) {
AM_PM = " PM";
if (hours >=13 && hours < 24) {
hours -= 12;
}
else {
hours = 12;
}
} else if (hours == 0) {
hours = 12;
}
if (minutes < 10) {
mm_precede = "
edittime.setText( hours + ":" + minutes +" "+ AM_PM);
}
}, hour, minute,
mTimePicker.
}
} );
答案 0 :(得分:2)
您应该以db格式(UNIX时间戳)存储日期,然后在从db中检索时以您希望的方式对其进行格式化。
答案 1 :(得分:0)
long inputTime;
Calendar mCalendar;
mCalendar = Calendar.getInstance();
//这里是TimePickerDialog
new TimePickerDialog(SearchActivity.this, onTimeSetListener,
mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false).show();
onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int min) {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, min);
inputTime = c.getTime().getTime();
}
};
//将此inputTime保存在db中作为long值。
//用于格式转换的实用程序方法
public static String getFormattedTime(long milliSeconds, String dateFormat) {
// Create a DateFormatter object for displaying date in specified format.
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
//从db读取后,将long值(inputTime)传递给getFormattedTime()方法以获取格式化时间。
String formatedTime = getFormattedTime(inputTime, Constants.TIME_12_HOUR_FORMAT);