MCQs
What is the output of this program?
import java.text.*;
import java.util.*;
class Date_formatting {
public static void main(String args[]) {
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("z");
System.out.print(sdf.format(date));
}
}
Note : The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time).
format string "z" is used to print time zone.
Output:
$ javac Date_formatting.java
$ java Date_formatting
PDT
What is the output of this program?
import java.text.*;
import java.util.*;
class Date_formatting {
public static void main(String args[]) {
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("E MMM dd yyyy");
System.out.print(sdf.format(date));
}
}
Note: The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time).
None.
Output:
$ javac Date_formatting.java
$ java Date_formatting
Mon Jul 15 2013
What is the output of this program?
import java.text.*;
import java.util.*;
class Date_formatting {
public static void main(String args[]) {
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("hh:mm:ss");
System.out.print(sdf.format(date));
}
}
Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time).
The code "sdf = new SimpleDateFormat("hh:mm:ss");" create a SimpleDataFormat
What is the output of this program?
import java.text.*;
import java.util.*;
class Date_formatting {
public static void main(String args[]) {
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("mm:hh:ss");
System.out.print(sdf.format(date));
}
}
Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time).
None.
Output:
$ javac Date_formatting.java
$ java Date_formatting
55:03:04
By using format string "w" we can print week in a year whereas by using 'W' we
By using format string "a" we can print AM/PM in time.
The DateFormat is a concrete subclass of DateFormat. It allows you to define your
own formatting patterns that are used to display date and time information.
java.text allows formatting, searching and manipulating text.
DateFormat is an abstract class that provides the ability to format and parse
dates and times.
getTimeInstance() method returns an instance of DateFormat that can format
time information.