Sie sind auf Seite 1von 55

1

 java.util.Date
 java.text.DateFormat
 java.text.SimpleDateFormat
 java.util.Properties
 java.util.TreeMap
 java.util.Hashtable
 java.util.Enumeration
 java.util.Vector
 java.util.Iterator

2
java.util.Date
 Date
 DateCalendar

3

 yy

4


5
 import java.util.Date;

 public class DateExample1
 {
 public static void main(String[] args)
 {
 // Get the system date/time
 Date date = new Date();

 System.out.println(date.getDate());
 System.out.println(date.getMonth() + 1);
 System.out.println(date.getYear() + 1900);
 System.out.println(date.getTime());
 }
 }

6
 C:\>javac DateExample1.java
 Note: DateExample1.java uses or overrides a
deprecated API.
 Note: Recompile with -Xlint:deprecation for
details.

 C:\>java DateExample1
 1
 12
 2005
 1133416404296

 C:\>

7
 java.text.DateFormat
java.text.SimpleDateFormat
java.util.Date

8
 Friday-December-02-2005

9
 import java.text.SimpleDateFormat;
 import java.util.Date;

 public class DateExample2


 {
 public static void main(String[] args)
 {
 SimpleDateFormat bartDateFormat = new
SimpleDateFormat("EEEE-MMMM-dd-yyyy");
 Date date = new Date();


System.out.println(bartDateFormat.format(date));
 }
 }

10
 C:\>javac DateExample2.java

 C:\>java DateExample2
 Thursday-December-01-2005

 C:\>

11
 EEEE-MMMM-dd-yyyy
 EEEEMMMMddyyyy
 EE-MM-dd-yyThu-12-01-05

12
 import java.text.DateFormat;
 import java.util.Date;

 public class DateFormatExample1


 {
 public static void main(String[] args)
 {
 // Make a new Date object. It will be
initialized to the current time.
 Date now = new Date();

 // See what toString() returns


 System.out.println(" 1. " + now.toString());

 // Next, try the default DateFormat


 System.out.println(" 2. " +
 DateFormat.getInstance().format(now));

13
 // And the default time and date-time DateFormats
 System.out.println(" 3. " +
DateFormat.getTimeInstance().format(now));
 System.out.println(" 4. " +
DateFormat.getDateTimeInstance().format(now));

 // Next, try the short, medium and long variants of the


 // default time format
 System.out.println(" 5. " +

DateFormat.getTimeInstance(DateFormat.SHORT).format(now));

 System.out.println(" 6. " +

DateFormat.getTimeInstance(DateFormat.MEDIUM).format(now));

 System.out.println(" 7. " +

DateFormat.getTimeInstance(DateFormat.LONG).format(now));

14
 // For the default date-time format, the length
of both the
 // date and time elements can be specified. Here
are some examples:
 System.out.println(" 8. " +
DateFormat.getDateTimeInstance(
 DateFormat.SHORT,
DateFormat.SHORT).format(now));
 System.out.println(" 9. " +
DateFormat.getDateTimeInstance(
 DateFormat.MEDIUM,
DateFormat.SHORT).format(now));
 System.out.println("10. " +
DateFormat.getDateTimeInstance(
 DateFormat.LONG,
DateFormat.LONG).format(now));
 }
 }

15
 C:\>javac DateFormatExample1.java

 C:\>java DateFormatExample1
 1. Thu Dec 01 16:39:04 GMT+05:30 2005
 2. 12/1/05 4:39 PM
 3. 4:39:04 PM
 4. Dec 1, 2005 4:39:04 PM
 5. 4:39 PM
 6. 4:39:04 PM
 7. 4:39:04 PM GMT+05:30
 8. 12/1/05 4:39 PM
 9. Dec 1, 2005 4:39 PM
 10. December 1, 2005 4:39:04 PM GMT+05:30

 C:\>

16
StringDate
 DateFormatgetInstance()
getTimeInstance()
getDateTimeInstance()Date

17
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.util.Date;

 public class DateFormatExample2


 {
 public static void main(String[] args)
 {
 // Make a String that has a date in it, with
MEDIUM date format
 // and SHORT time format.
 String dateString = "Nov 23, 1976 5:14 PM";

 // Get the default MEDIUM/SHORT DateFormat


 DateFormat format =
 DateFormat.getDateTimeInstance
(DateFormat.MEDIUM, DateFormat.SHORT);

18
 // Parse the date
 try
 {
 Date date = format.parse(dateString);
 System.out.println("Original string: " +
dateString);
 System.out.println("Parsed date : " +
date.toString());
 }
 catch(ParseException pe)
 {
 System.out.println("ERROR: could not
parse date in string \"" + dateString + "\"");
 }
 }
 }

19
 C:\>javac DateFormatExample2.java

 C:\>java DateFormatExample2
 Original string: Nov 23, 1976 5:14 PM
 Parsed date : Tue Nov 23 17:14:00
GMT+05:30 1976

 C:\>

20
 parse
 DateFormatExample2

21
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.util.Date;
 import java.io.IOException;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;

 public class DateFormatExample3


 {
 public static void main(String[] args)
 {
 // Get the default MEDIUM/SHORT DateFormat
 DateFormat format =
 DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.SHORT);

 // Read and parse input, stopping on a blank input line


 BufferedReader reader =
 new BufferedReader(new InputStreamReader(System.in));

22
 try
 {
 System.out.print("Enter Date String [Ex - Nov
23, 1976 5:14 PM]:");
 String dateString = reader.readLine();

 while ((dateString != null) &&


(dateString.length() > 0))
 {
 // Parse the date
 try
 {
 Date date =
format.parse(dateString);
 System.out.println("Original
string: " + dateString);
 System.out.println("Parsed
date : " + date.toString());
 System.out.println(); // Skip
a line
 }

23
 catch(ParseException pe)
 {
 System.out.println
("ERROR: could not parse date in string \"" +
dateString + "\"");
 }

 // Read another string


 System.out.print("Enter
Date String [Ex - Nov 23, 1976 5:14 PM]:");
 dateString =
reader.readLine();
 }
 }

24
 catch(IOException ioe)
 {

System.out.println("I/O
Exception: " + ioe);
 }
 }
}

25
 C:\>javac DateFormatExample3.java

 C:\>java DateFormatExample3
 Enter Date String [Ex - Nov 23, 1976 5:14 PM]:01-12-
2005 2:00 PM
 ERROR: could not parse date in string "01-12-2005
2:00 PM"
 Enter Date String [Ex - Nov 23, 1976 5:14 PM]:Dec 1,
2005 5:06 PM
 Original string: Dec 1, 2005 5:06 PM
 Parsed date : Thu Dec 01 17:06:00 GMT+05:30 2005

 Enter Date String [Ex - Nov 23, 1976 5:14 PM]:

 C:\>

26
java.util.Properties

 Properties
 Properties

27
 import java.util.*;
 import java.io.*;

 public class PropsList


 {
 public static void main (String args[]) throws Exception
 {
 Properties p = System.getProperties();
 p.list(System.out);

 FileOutputStream fos = new


FileOutputStream("sys.out");
 p.store(fos, null);
 fos.close();

 Map map = new TreeMap(p);
 System.out.println(map);
 }
 }

28
 C:\>javac PropsList.java
 Note: List.java uses unchecked or unsafe operations.
 Note: Recompile with -Xlint:unchecked for details.

 C:\>java PropsList
 -- listing properties --
 java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
 sun.boot.library.path=C:\Program Files\Java\jre1.5.0_04\bin
 java.vm.version=1.5.0_04-b05
 java.vm.vendor=Sun Microsystems Inc.
 java.vendor.url=http://java.sun.com/
 path.separator=;
 java.vm.name=Java HotSpot(TM) Client VM
 file.encoding.pkg=sun.io
 user.country=US
 sun.os.patch.level=Service Pack 4
 java.vm.specification.name=Java Virtual Machine Specification
 user.dir=C:\
 java.runtime.version=1.5.0_04-b05
 …………………………………………………………………
 …………………………………………………………………

29
java.util.TreeMap

 natural order

30
 import java.util.*;

 public class tSortedMap


 {
 public static void main(String[] args)
 {
 Map m = new TreeMap();
 StringBuffer sb = null;

 m.put("Basha",new Integer(101));
 m.put("Zen",new Integer(102));
 m.put("Madhav",new Integer(110));
 m.put("Arun",new Integer(107));
 m.put("Peter",new Integer(104));

31
 for (Iterator i =
m.keySet().iterator(); i.hasNext();)
 {
 sb = new
StringBuffer((String)i.next());
 System.out.println(sb + " "
+ m.get(sb.toString()));
 }
 }
 }

32
 C:\>javac tSortedMap.java
 Note: tSortedMap.java uses unchecked or unsafe
operations.
 Note: Recompile with -Xlint:unchecked for
details.

 C:\>java tSortedMap
 Arun 107
 Basha 101
 Madhav 110
 Peter 104
 Zen 102

 C:\>

33
java.util.Hashtable


 Hashtable numbers = new Hashtable();
 numbers.put("one", new Integer(1));

 numbers.put("two", new Integer(2));

 numbers.put("three", new Integer(3));

34

 Integer n = (Integer)numbers.get("two");
 if (n != null)
 {
 System.out.println("two = " + n);
 }

 Hashtable

35
java.util.Enumeration
 public interface Enumeration
 EnumerationnextElement
 v
for (Enumeration e = v.elements();
e.hasMoreElements();)
{
System.out.println(e.nextElement());
}

36

 SequenceInputStream

37

 Iterator

 Iterator

 IteratorEnumeration

38
 import java.util.*;

 class UseEnum
 {
 public static void main(String args[])
 {
 Hashtable ht = new Hashtable();
 // Hashtable is used to store key-value
pairs
 // this speeds up the process of searching
for an element

 String s1= new String("keyone");


 String s2= new String("keytwo");
 String s3= new String("keythree");
 // any java object can be used as a key,
but here we are using string objects

39
 ht.put(s1,"value one");
 ht.put(s2,"value two");
 ht.put(s3,"value three");
 // we can use any java object as
value

 System.out.println(ht.get(s3));
 ht.put(s3,"new three");
 System.out.println(ht.get(s3));

40
 // let us use enumerator here
 Enumeration e = ht.keys();
 // returns an enumeration object which
 // can used to iterate through list.
 System.out.println(e);

 while (e.hasMoreElements())
 {
 System.out.println(e.nextElement());
 }
 }
 }

41
 C:\>javac UseEnum.java
 Note: UseEnum.java uses unchecked or unsafe
operations.
 Note: Recompile with -Xlint:unchecked for
details.

 C:\>java UseEnum
 value three
 new three
 java.util.Hashtable$Enumerator@10b62c9
 keythree
 keytwo
 keyone

42
java.util.Vector
 Vector

 VectorVector

43
 VectorVector
 capacitycapacityIncrement

44
 capacitycapacityIncrement

45
 import java.util.*;

 class UseVectorEnum
 {
 public static void main(String args[])
 {
 Vector v = new Vector();
 v.addElement(new String("one"));
 v.addElement(new String("two"));
 v.addElement(new String("three"));
 v.addElement(new String("four"));
 // we can add any java object to vector

 // let us use enumerator here


 Enumeration e = v.elements();

 while (e.hasMoreElements())
 {
 System.out.println(e.nextElement());
 }
 }
 }

46
 C:\>javac UseVectorEnum.java
 Note: UseVectorEnum.java uses unchecked or
unsafe operations.
 Note: Recompile with -Xlint:unchecked for
details.

 C:\>java UseVectorEnum
 one
 two
 three
 four

 C:\>

47
 import java.util.Vector;

 public class FindVector


 {
 public static void main(String args[])
 {
 String data[] = { "Java", "Source", "and",
"Support", "."};
 Vector v = new Vector();

 for (int i=0, n=data.length; i<n; i++)


 {
 v.add(data[i]);
 }

48
 System.out.println("Vector: " + v);

 System.out.println("Contains Java?: "


+ v.contains("Java"));
 System.out.println("Contains Java2s?:
" + v.contains("Java2s"));
 System.out.println("Where's and?: " +
v.indexOf("and"));
 System.out.println("Where's Source?: "
+ v.indexOf("Source"));
 System.out.println("Where's Java from
end?: " + v.lastIndexOf("Java"));
 }
 }

49
 C:\>javac FindVector.java
 Note: FindVector.java uses unchecked or unsafe
operations.
 Note: Recompile with -Xlint:unchecked for details.

 C:\>java FindVector
 Vector: [Java, Source, and, Support, .]
 Contains Java?: true
 Contains Java2s?: false
 Where's and?: 2
 Where's Source?: 1
 Where's Java from end?: 0

 C:\>

50
java.util.Iterator
 public interface Iterator
 Iterator

51
 import java.util.*;

 class UseVectorIter
 {
 public static void main(String args[])
 {
 Vector v = new Vector();
 v.addElement(new String("one"));
 v.addElement(new String("two"));
 v.addElement(new String("three"));
 v.addElement(new String("four"));
 // we can add any java object to vector

 // let us use iterator here


 Iterator e = v.iterator();

 while (e.hasNext())
 {
 System.out.println(e.next());
 }
 }
 }

52
 C:\>javac UseVectorIter.java
 Note: UseVectorEnum.java uses unchecked or
unsafe operations.
 Note: Recompile with -Xlint:unchecked for
details.

 C:\>java UseVectorIter
 one
 two
 three
 four

 C:\>

53



54

Thank You…
55

Das könnte Ihnen auch gefallen