0% found this document useful (0 votes)
42 views

File 5 (Java DateTime and String) P1

The document discusses Java date and time classes and methods. It introduces the Date class for getting the current date and time. SimpleDateFormat allows formatting dates with custom patterns. Common format codes are listed for hours, minutes, years, etc. Strings in Java are objects that provide methods like length(), concat(), and trim().

Uploaded by

Riduan Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

File 5 (Java DateTime and String) P1

The document discusses Java date and time classes and methods. It introduces the Date class for getting the current date and time. SimpleDateFormat allows formatting dates with custom patterns. Common format codes are listed for hours, minutes, years, etc. Strings in Java are objects that provide methods like length(), concat(), and trim().

Uploaded by

Riduan Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java - Date and Time

Java provides the Date class available in java.util package, this class encapsulates the


current date and time.
Getting Current Date and Time
This is a very easy method to get current date and time in Java. You can use a simple Date
object with toString() method to print the current date and time as follows −

Example
import java.util.Date;
public class DateDemo {

public static void main(String args[]) {


// Instantiate a Date object
Date date = new Date();

// display time and date using toString()


System.out.println(date.toString());
}
}
Output
on May 04 09:51:52 CDT 2009
Date Formatting Using SimpleDateFormat
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive
manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for
date-time formatting.

Example
import java.util.*;
import java.text.*;

public class DateDemo {

public static void main(String args[]) {


Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("E yyyy.MM.dd 'at'
hh:mm:ss a zzz");

System.out.println("Current Date: " +


ft.format(dNow));
}
}

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

y Year in four digits 2001

M Month in year July or 07

d Day in month 10

h Hour in A.M./P.M. (1~12) 12

H Hour in day (0~23) 22

m Minute in hour 30

s Second in minute 55

S Millisecond 234

E Day in week Tuesday

D Day in year 360

F Day of week in month 2 (second Wed. in July)

w Week in year 40

W Week in month 1

a A.M./P.M. marker PM

k Hour in day (1~24) 24

K Hour in A.M./P.M. (0~11) 10

z Time zone Eastern Standard Time

' Escape for text Delimiter

" Single quote `


Java - Strings Class
Strings, which are widely used in Java programming, are a sequence of characters.
In Java programming language, strings are treated as objects.
The Java platform provides the String class to create and manipulate strings.

Creating Strings
The most direct way to create a string is to write −
String greeting = "Hello world!";

Example
public class StringDemo {

public static void main(String args[]) {


char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
System.out.println( helloString );
}
}

This will produce the following result −

Output
hello.

String Length
Methods used to obtain information about an object are known as accessor
methods.

Example
public class StringDemo {

public static void main(String args[]) {


String palindrome = "Depatrment Of CSE";
int len = palindrome.length();
System.out.println( "String Length is : " + len );
}
}

This will produce the following result −

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 {

public static void main(String args[]) {


String string1 = "saw I was ";
System.out.println("Dot " + string1 + "Tod");
}
}
This will produce the following result −
Output
Dot saw I was Tod
String Methods
Here is the list of methods supported by String class −
Method & Description Example
char charAt(int index) public static void main(String args[]) {
Returns the character at the specified String s = "Strings are immutable";
index. char result = s.charAt(8);
System.out.println(result);
}
int compareTo(String anotherString) public static void main(String args[]) {
String str1 = "Strings are immutable";
Compares two strings lexicographically.
String str2 = "Strings are immutable";

int result = str1.compareTo( str2 );


System.out.println(result);
}
boolean endsWith(String suffix) public static void main(String args[]) {
Tests if this string ends with the specified
suffix. }

String Str = new String("This is really not immutable!!");


boolean retVal;

retVal = Str.endsWith( "immutable!!" );


System.out.println("Returned Value = " + retVal );
int hashCode() public static void main(String args[]) {
String Str = new String("Welcome to CBIU");
Returns a hash code for this string.
System.out.println("Hashcode for Str :" +
Str.hashCode() );
}
String toLowerCase() public static void main(String args[]) {
String Str = new String("Welcome to CBIU");
Converts all of the characters in this
String to lower case using the rules of System.out.print("Return Value :");
the default locale. System.out.println(Str.toLowerCase());
}
String toUpperCase() public static void main(String args[]) {
String Str = new String("Welcome to cbiu");
Converts all of the characters in this
String to upper case using the rules of
the default locale. System.out.print("Return Value :" );
System.out.println(Str.toUpperCase() );
}

String trim() public static void main(String args[]) {


String Str = new String("Welcome to cbiu ");
Returns a copy of the string, with
leading and trailing whitespace omitted.
System.out.print("Return Value :" );
System.out.println(Str.trim() );
}

You might also like