我想把日期分成小时和分钟。我在Android应用中使用了这段代码:
reminderTextField.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
new SingleDateAndTimePickerDialog.Builder(view.getContext())
.minutesStep(1)
.setDayFormatter(formatter)
.mainColor(view.getResources().getColor(R.color.colorPrimary))
.titleTextColor(Color.BLACK)
.displayListener(new SingleDateAndTimePickerDialog.DisplayListener() {
@Override
public void onDisplayed(SingleDateAndTimePicker picker) {})
.mustBeOnFuture()
.title("Set Reminder")
.listener(new SingleDateAndTimePickerDialog.Listener() {
@Override
public void onDateSelected(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
String reminderTime = formatter.format(date);
reminderTextField.setText(reminderTime);
}
}).display();
}
答案 0 :(得分:0)
使用Calendar
类来执行以下操作:
java.util.Date date= new Date("Your date here");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH); // Month
int day = cal.get(Calendar.DAY_OF_MONTH); // Day
int year = cal.get(Calendar.YEAR); // Year
int hour = cal.get(Calendar.HOUR_OF_DAY); // Hour
int minute = cal.get(Calendar.MINUTE); // Minute
...
如果您需要更多内容,则所有课程Calendar
都包含Oracle documentation上的方法
然后由你决定;)
提示:使用System.out.println()
方法在控制台上查看结果,并确保它符合您的要求。