Write the missing code in the below program. A calendarDemo class is created with a setTime method which passes one argument. SimpleDateFormat is used to parse the string and produce the date. Calendar setTime method is used to set time to the value of date.
Write the missing code in the below program. A calendarDemo class is created with a setTime method which passes one argument. SimpleDateFormat is used to parse the string and produce the date. Calendar setTime method is used to set time to the value of date.
Epected Output
Setting·to·the·new·time·Feb-09-2012
DRIVER CODE
package q11589;
import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class CalendarDemoMain {
public static void main(String ... args) {
String date = args[0];
CalendarDemo c = new CalendarDemo();
try {
String date2 = c.setTime(date);
System.out.println("Setting to the new time " + date2);
}
catch(ParseException pe) {
System.out.println("You used the wrong format in SimpleDateFormat");
}
}
}
CHANGE IN THIS CODE
package q11589;
import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class CalendarDemo {
public String setTime(String dateString) throws ParseException {
// use the correct format string as argument for the constructor
SimpleDateFormat sd = new SimpleDateFormat("");
// parses text from the beginning of the given string to produce a date
Date date = sd.parse(dateString);
Calendar currentTime = Calendar.getInstance();
//// configuring the current object to the value of date
currentTime.setTime(date);
// format method on SimpleDateFormat returns a string representation of the passed date
return sd.format(currentTime.getTime());
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images