0
Java error
I am trying to take a inputed date from user and add 10 days to it but I keep get this error... (Assignment conversion not possible from type “java.lang.string” to type “java.until.calender”) Please help
5 Antworten
+ 6
Can you please show us the code snippet so that we can suggest potential fixes? From the looks of it, you just need to cast your input to date.
+ 2
I made this. It's correct, unless you want custom formatting. :3
import java.util.Scanner;
import java.util.Calendar;
import java.util.Date;
public class TestProject {
public static void main(String[] args){
Scanner date = new Scanner(System.in);
System.out.println("Enter Date mm/dd/yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(date.nextLine()));
cal.add(Calendar.DATE, 10);
System.out.println("10 days later: " + cal.getTime());
}
}
Further reading:
https://www.mkyong.com/java/java-date-and-calendar-examples/
+ 1
date.nextLine() will return string value, you trying to assign it to variable with Calendar type. Here is correct way to do it https://stackoverflow.com/questions/5301226/convert-string-to-calendar-object-in-java
0
import java.util.Scanner;
import java.util.Calendar;
public class TestProject {
public static void main(String[] args){
Scanner date = new Scanner(System.in);
System.out.println("Enter Date mm/dd/yyyy");
date.nextLine();
Calendar cal = date.nextLine();
cal.add(Calendar.DATE, 10);
System.out.println("10 days later: " + cal.getTime());
0
Thank you all