从字符串格式转换后无法打印时间
Could not print time after conversion from string format
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
// For Time validation
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
我正在努力将我拥有的字符串转换为日期和时间格式。例如,字符串 datechosen 包含“26/10/2020”。我可以将它转换成日期格式并打印出来。
但是对于时间字符串,我无法打印出来。我面临以下错误:
Screenshot of the log message
但是如果我反过来交换代码的位置,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
将改为打印时间
这是两个输入字段
您为时间设置的日期格式有误。对于 cal.setTime(timeselected);
setTime 需要 Date
Refer java doc
使用与日期相同的格式。
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
// For Time validation
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
我正在努力将我拥有的字符串转换为日期和时间格式。例如,字符串 datechosen 包含“26/10/2020”。我可以将它转换成日期格式并打印出来。
但是对于时间字符串,我无法打印出来。我面临以下错误:
Screenshot of the log message
但是如果我反过来交换代码的位置,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
将改为打印时间
您为时间设置的日期格式有误。对于 cal.setTime(timeselected);
setTime 需要 Date
Refer java doc
使用与日期相同的格式。