File 5 (Java DateTime and String) P1
File 5 (Java DateTime and String) P1
Example
import java.util.Date;
public class DateDemo {
Example
import java.util.*;
import java.text.*;
Output
Current Date: Sun 2004.07.18 at 04:14:09 PM PDT
Simple DateFormat Format Codes
To specify the time format, use a time pattern string. In this pattern, all ASCII letters
are reserved as pattern letters, which are defined as the following −
Character Description Example
G Era designator AD
d Day in month 10
m Minute in hour 30
s Second in minute 55
S Millisecond 234
w Week in year 40
W Week in month 1
a A.M./P.M. marker PM
Creating Strings
The most direct way to create a string is to write −
String greeting = "Hello world!";
Example
public class StringDemo {
Output
hello.
String Length
Methods used to obtain information about an object are known as accessor
methods.
Example
public class StringDemo {
Output
String Length is : 17
Concatenating Strings
The String class includes a method for concatenating two strings −
string1.concat(string2);
This returns a new string that is string1 with string2 added to it at the end. You can
also use the concat() method with string literals, as in −
"My name is ".concat("Zara");
Strings are more commonly concatenated with the + operator, as in −
"Hello," + " world" + "!"
which results in −
"Hello, world!"
Let us look at the following example −
Example
public class StringDemo {