226k views
5 votes
Assume the variable date has been set to a string value of the form mm/dd/yyyy, for example 09/08/2010. (Actual numbers would appear in the string.) Write a statement to assign to a variable named dayStr the characters in date that contain the day. Then set a variable day to the integer value corresponding to the two digits in dayStr.

1 Answer

0 votes

Answer:

String date = "21/05/2020";

String dayStr = date.substring(0,2);

int day = Integer.parseInt(dayStr);

System.out.println(day);

Step-by-step explanation:

Create a variable called date which holds the current date

Create a variable called dayStr. Initialize it to the day part of the date using the substring method

Create a variable called day. Parse the dayStr and assign it to the day

Print the day

User RobW
by
3.8k points