3 回答
TA贡献1779条经验 获得超6个赞
有一个类级别的 Calendar 实例,让我们像之前那样实例化它:
Calendar combinedCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
因此,当您选择日期时,请按照您的方式设置值:
combinedCal.set(year, monthOfYear, dayOfMonth);
选择时间时,只需将选定的小时和分钟设置为同一实例:
combinedCal.set(Calendar.HOUR_OF_DAY, selectedHour); combinedCal.set(Calendar.MINUTE, selectedMinute);
TA贡献1851条经验 获得超5个赞
Timestamp 包含您从 Datepicker 和 Timepicker 中选择的日期和时间
timepicker = (Timepicker)findViewById(R.id.timepicker);
datepicker = (Datepicker)findViewById(R.id.datepicker);
int year = datepicker.getYear();
int month = datepicker.getMonth();
int day = datepicker.getDay();
int hour= timepicker.getCurrentHour();
int minute = timepicker.getCurrentMinute();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Long timestamp = calendar.getTimeInMills();
System.out.println(timestamp);
TA贡献1794条经验 获得超7个赞
我在获取日期时遇到了类似的问题,所以我所做的是创建一个字符串,用于选择当前日期和时间,然后将其合并以创建一个完美的时间戳,就像这样
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(ServiceManDetails.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String month,day;
if(monthOfYear<9){
month="0"+ (monthOfYear + 1);
if(dayOfMonth<10){
day="0"+dayOfMonth;
}else{
day=dayOfMonth+"";
}
}else{
month= (monthOfYear + 1)+"";
if(dayOfMonth<10){
day="0"+dayOfMonth;
}else{
day=dayOfMonth+"";
}
}
//String selectedday=dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
pickedDate=year + "/" + month + "/" + day;
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(ServiceManDetails.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
String hr,min;
if(hourOfDay<10){
hr="0"+hourOfDay;
if(minute<10){
min="0"+minute;
}else{
min=minute+"";
}
}else{
hr=hourOfDay+"";
if(minute<10){
min="0"+minute;
}else{
min=minute+"";
}
}
time=hr + ":" + min + ":00";
date=pickedDate+" "+time;
scheduleDialog(date);
}
}, mHour, mMinute, true);
timePickerDialog.show();
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
我也想设置毫秒,所以我设置日期自动将秒设置为 00 你也可以将毫秒添加为 000
添加回答
举报