Tuesday 28 March 2017

The Core Java API Packages & Text Formatting - Java Tutorials

The Core Java API Packages

The Core Java API Packages

java.applet:  Supports construction of applets.

java.awt:  Provides capabilities for graphical user interfaces.

java.awt.color:  Supports color spaces and profiles.

java.awt.datatransfer:  Transfers data to and from the system clipboard.

java.awt.dnd:  Supports drag-and-drop operations.

java.awt.event:  Handles events.

java.awt.font:  Represents various types of fonts.

java.awt.geom:  Allows you to work with geometric shapes.

java.awt.im:  Allows input of Japanese, Chinese, and Korean characters to text editing components.

java.awt.im.spi:  Supports alternative input devices. (Added by Java 2, v1.3)

java.awt.image:  Processes images.

java.awt.image.renderable:  Supports rendering-independent images.

java.awt.print:  Supports general print capabilities.

java.beans:  Allows you to build software components.

java.beans.beancontext:  Provides an execution environment for beans.

java.io:  Inputs and outputs data.

java.lang:  Provides core functionality.

java.lang.ref:  Enables some interaction with the garbage collector.

java.lang.reflect:  Analyzes code at run time.

java.math:  Handles large integers and decimal numbers.

java.net:  Supports networking.

java.nio:  Top-level package for the new Java I/O classes. Encapsulates buffers. (Added by Java 2, v1.4)

java.nio.channels:  Encapsulates channels, which are used by the new I/O system. (Added by Java 2, v1.4)

java.nio.channels.spi:  Supports service providers for channels. (Added by Java 2, v1.4)

java.nio.charset:  Encapsulates character sets, which are used by the new I/O system. (Added by Java 2, v1.4)

java.nio.charset.spi:  Supports service providers for charsets. (Added by Java 2, v1.4)

java.rmi:  Provides remote method invocation.

java.rmi.activation:  Activates persistent objects.

java.rmi.dgc:  Manages distributed garbage collection.

java.rmi.registry:  Maps names to remote object references.

java.rmi.server:  Supports remote method invocation.

java.security:  Handles certificates, keys, digests, signatures, and other security functions.

java.security.acl:  Manages access control lists.

java.security.cert:  Parses and manages certificates.

java.security.interfaces:  Defines interfaces for DSA (Digital Signature Algorithm) keys.

java.security.spec:  Specifies keys and algorithm parameters.

java.sql:  Communicates with a SQL (Structured Query Language) database.

java.text:  Formats, searches, and manipulates text.

java.util:  Contains common utilities.

java.util.jar:  Creates and reads JAR files.

java.util.logging:  Supports logging of information related to a program’s execution. (Added by Java 2, v1.4)

java.util.prefs:  Encapsulates information relating to user preferences. (Added by Java 2, v1.4)

java.util.regex:  Supports regular expression processing. (Added by Java 2, v1.4)

java.util.zip:  Reads and writes compressed and uncompressed ZIP files.




Text Formatting

The package java.text allows you to format, search, and manipulate text. This section takes a brief look at its most commonly used classes: those that format date and time information.


DateFormat Class

DateFormat is an abstract class that provides the ability to format and parse dates and times. The getDateInstance( ) method returns an instance of DateFormat that can format date information. It is available in these forms:

      static final DateFormat getDateInstance( )
      static final DateFormat getDateInstance(int style)
      static final DateFormat getDateInstance(int style, Locale locale)

The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. They cause different details about the date to be presented. The argument locale is one of the static references defined by Locale (refer to Chapter 16 for details). If the style and/or locale is not specified, defaults are used.

One of the most commonly used methods in this class is format( ). It has several overloaded forms, one of which is shown here:

      final String format(Date d)

The argument is a Date object that is to be displayed. The method returns a string containing the formatted information.

The following listing illustrates how to format date information. It begins by creating a Date object. This captures the current date and time information. Then it outputs the date information by using different styles and locales.

  // Demonstrate date formats.
  import java.text.*;
  import java.util.*;

  public class DateFormatDemo {
    public static void main(String args[]) {
      Date date = new Date();
      DateFormat df;

      df = DateFormat.getDateInstance(DateFormat.SHORT, 
                                      Locale.JAPAN);
      System.out.println("Japan: " + df.format(date));

      df = DateFormat.getDateInstance(DateFormat.MEDIUM, 
                                      Locale.KOREA);
      System.out.println("Korea: " + df.format(date));

      df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK);
      System.out.println("United Kingdom: " + df.format(date));
  
      df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
      System.out.println("United States: " + df.format(date));
    }
  }

Sample output from this program is shown here:

  Japan: 02/05/08
  Korea: 2002-05-08
  United Kingdom: 08 May 2002
  United States: Wednesday, May 8, 2002

The getTimeInstance( ) method returns an instance of DateFormat that can format time information. It is available in these versions:

      static final DateFormat getTimeInstance( )
      static final DateFormat getTimeInstance(int style)
      static final DateFormat getTimeInstance(int style, Locale locale)

The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. They cause different details about the time to be presented. The argument locale is one of the static references defined by Locale. If the style and/or locale is not specified, defaults are used.

The following listing illustrates how to format time information. It begins by creating a Date object. This captures the current date and time information and then outputs the time information by using different styles and locales.

  // Demonstrate time formats.
  import java.text.*;
  import java.util.*;
  public class TimeFormatDemo {
    public static void main(String args[]) {
      Date date = new Date();
      DateFormat df;

      df = DateFormat.getTimeInstance(DateFormat.SHORT, 
                                      Locale.JAPAN);
      System.out.println("Japan: " + df.format(date));

      df = DateFormat.getTimeInstance(DateFormat.LONG, Locale.UK);
      System.out.println("United Kingdom: " + df.format(date));

      df = DateFormat.getTimeInstance(DateFormat.FULL, 
                                      Locale.CANADA);
      System.out.println("Canada: " + df.format(date));
    }
  }

Sample output from this program is shown here:

  Japan: 20:25
  United Kingdom: 20:25:14 CDT
  Canada: 8:25:14 o'clock PM CDT

The DateFormat class also has a getDateTimeInstance( ) method that can format both date and time information. You may wish to experiment with it on your own.


SimpleDateFormat Class

SimpleDateFormat is a concrete subclass of DateFormat. It allows you to define your own formatting patterns that are used to display date and time information.

One of its constructors is shown here:

      SimpleDateFormat(String formatString)

The argument formatString describes how date and time information is displayed. An example of its use is given here:

SimpleDateFormat sdf = SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");

In most cases, the number of times a symbol is repeated determines how that data is presented. Text information is displayed in an abbreviated form if the pattern letter is repeated less than four times. Otherwise, the unabbreviated form is used. For example, a zzzz pattern can display Pacific Daylight Time, and a zzz pattern can display PDT.


Formatting String Symbols for SimpleDateFormat

a    -   AM or PM
d    -   Day of month (1–31)
h    -   Hour in AM/PM (1–12)
k    -   Hour in day (1–24)
m   -   Minute in hour (0–59)
s    -    Second in minute (0–59)
w   -   Week of year (1–52)
y    -   Year
z    -    Time zone
D   -    Day of year (1–366)
E    -    Day of week (for example, Thursday)
F    -    Day of week in month
G   -    Era (that is, AD or BC)
H   -    Hour in day (0–23)
K   -    Hour in AM/PM (0–11)
M   -   Month
S    -    Millisecond in second
W   -   Week of month (1–5)
Z    -    Time zone in RFC822 format

For numbers, the number of times a pattern letter is repeated determines how many digits are presented. For example, hh:mm:ss can present 01:51:15, but h:m:s displays the same time value as 1:51:15.

Finally, M or MM causes the month to be displayed as one or two digits. However, three or more repetitions of M cause the month to be displayed as a text string. The following program shows how this class is used:

  // Demonstrate SimpleDateFormat.
  import java.text.*;
  import java.util.*;

  public class SimpleDateFormatDemo {
    public static void main(String args[]) {
      Date date = new Date();
      SimpleDateFormat sdf;
      sdf = new SimpleDateFormat("hh:mm:ss");
      System.out.println(sdf.format(date));
      sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
      System.out.println(sdf.format(date));
      sdf = new SimpleDateFormat("E MMM dd yyyy");
      System.out.println(sdf.format(date));
    }
  }

Sample output from this program is shown here:

  02:18:23
  08 May 2002 02:18:23 CDT
  Wed May 08 2002

No comments:

Post a Comment