Thursday, October 21, 2004

Code For Using DecimalFormat

public String getCreditAmountFormat() {
DecimalFormat df = new DecimalFormat("00000000000000,000");
df.setMinimumIntegerDigits(14);
df.setMinimumFractionDigits(3);
df.setGroupingUsed(false);
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator(',');
df.setDecimalFormatSymbols(dfs);
return df.format(creditAmount.doubleValue());
}

Friday, October 08, 2004

Java 5 features

The new features in Java 5 can roughly be organized into four categories: enhanced language features, Virtual Machine upgrades, library updates, and desktop (UI) enhancements.

Language Features
There is a long list of language features that could be mentioned, but we will only take time to address some of the most significant developments.
Enumerated types – An enumerated type is a datatype containing a fixed set of constant values. As implemented in J2SE 5.0, enums are object-oriented (users can define methods and fields) and typesafe.

Formatted I/O – Console output has been enhanced by implementing printf-style formatters, enabling many legacy C applications to be ported without changing the expected output format. On the input side of the house, the Scanner API has been provided to offer developers a more robust mechanism for reading in data types rather than simply parsing strings from buffered System.in calls.

Generic types – Perhaps the most significant enhancements made to the Java language revolve around the introduction of generic types. Gone are the days when Java developers must toil and sweat to cast objects up and down the type hierarchy. With generic types, a collection’s type can be specified at compile time to permit compile type checks and ease development by providing implicit type casting. Additionally, primitive types are now able to automatically convert (autoboxing/unboxing) between the primitive type and its corresponding Object wrapper (int <----> Integer, boolean <----> Boolean, char <----> Char, etc.).

Metadata – J2SE 5.0 now supports the inclusion of metadata related to Java classes, interfaces, fields, and methods. Development tools are the intended consumers for this metadata to support the automated insertion of additional source code, debugging functionality, and creation/modification of external XML files or configuration files to support application runtime and deployment.

Varargs – With J2SE 5.0, support for passing a variable number of arguments into a method has been added. By using a special notation (…) in the method signature’s parameter list, an ad hoc Object array can be created.

Java Library

There are two new Java libraries, and a host of updates to existing Java libraries. The new libraries are the Java Concurrency Utilities (JSR-166) which provide advanced multithreading controls, and the JVM Profiling API (JSR-163), which is a more powerful native profiling API called JVMTI.
The concurrency utilities provide advanced, high-level multithreading capabilities such as thread-safe queues, a thread task framework through executors, locks (including semaphores), timers, and various other synchronization primitives. For more details, explore the JavaDocs under java.lang.concurrent.
The JVMTI implementation includes Java Programming Language Instrumentation Services (JPLIS) which function at the bytecode level to enable analysis tools to add profiling, monitoring, and debugging where it is needed. This technique provides great flexibility and limits the interference of profiling and monitoring tools on a currently executing JVM. For more details, explore the JavaDocs under java.lang.instrument. A wide range of other existing Java libraries have received important updates, including the Utility package, Networking, Security, RMI, JDBC, and the CORBA libraries.

Desktop (UI)

J2SE 5.0 has taken some significant strides to address the traditionally slow startup time and heavy-weight memory footprint that has traditionally plagued Java desktop applications. The new version boasts enhanced font capabilities (especially to support localization and internationalization), many updates to Java Sound (broader platform support, optimized direct audio access, an improved and real-time Sequencer, etc.), hardware-accelerated rendering for Java 2D using OpenGL drivers, and even a new skin changing look and feel (Synth) and Theme (Ocean) for Swing.

Virtual Machine

Error Handling – Although the improvements are simple, the value is tremendous. Java developers now have the ability to capture stack traces as String objects and use them however they need to using the getStackTrace and Thread.getAllStackTraces, rather than being forced to send Stack Trace information to the console. Additionally, the HotSpot JVM has been updated to include a fatal error handler for running a user-supplied script or program if the JVM dies. Finally, the HotSpot JVM has been given a serviceability agent connector that permits monitoring and debugging tools to connect to a hung JVM or core file and extract relevant diagnostic data.

Management and Monitoring – From instrumentation (JPLIS) to Java Management Extensions (JMX) support, to support of remote access protocols and SNMP tools, management and monitoring have been greatly enhanced with J2SE 5.0. All of this is ready to use out of the box, supporting local and remote management and monitoring of a running JVM.

Performance and Scalability – A great deal of attention has been given to the subject of performance in this latest release of the core Java platform. Java 5 starts more quickly, leaves a smaller memory footprint, and offers enhanced scalability by enabling multiple JVMs to share read-only data. One really interesting enhancement is the ability to designate the role of a JVM as either a ‘client’ or a ‘server’. The JVM then automatically tunes itself to provide the optimum environment for the specified role, delivering enhanced performance, garbage collection, and thread prioritization.

Analysis of Java 5

Overall, J2SE 5.0 has really aimed to make developing Java software easier. From language features that save time and effort (generics, formatted I/O, metadata, varargs, etc.), to enhanced error handling, JVM management, performance and scalability, and many Java library updates to ease common developer headaches.
With the inclusion of generics, overhauled JVM, and emphasis upon performance and scalability of the platform, the Sun engineering team addressed all the major items on my list of desired improvements to the Java platform. The only other thing that I really wished I had seen with this release was an enhanced Java Reference API to support more object-oriented caching capabilities within the platform straight out of the box (rather than relying upon this value-add from external vendors).

Conclusion

Java has come a long way over the past nine years. The strides made in this new release of the Java platform will no doubt be significant for the viability of the platform. It is critical that Sun shows the industry that Java is still an innovative platform that is robust enough and flexible enough to meet the ever-changing climate of the Information Technology sector. If the industry takes a close look at Java 5, I believe they will see that this is, in fact, the case. Go Tiger – 1, 2, …5!

Taming Tiger: Formatted output - Let there be printf

The original plan for the 1.4 release J2SE included support for formatted output. Probably due to time constraints and the fact that the feature wasn't a release driver, the capabilities were left out of the release. Now with Tiger, there is built in support for printing with format strings.

For those who grew up with Java programming and never touched C or those who haven't lived in a C world for some time, format strings are those funky text strings that specify output characteristics for a bunch of variables. Instead of just concatenating strings together with the plus sign (as in firstName + " " + lastName), you provide a single string to describe the output and provide the arguments to fill the placeholders in that string at the end of the method call: String s = String.format("%1$s %2$s", firstName, lastName).

Formatter class
First, let's look at the new java.util.Formatter class. You probably won't use this class directly much, but it provides the guts for the formatting you'll be doing. In the Javadoc for this class, you'll find a table describing the supported formatting options. These options range from something like %7.4f for specifying the precision and width of a floating point number to %tT for formatting a time to %3$s for formatting the third argument.

Using Formatter to format output involves two steps: creating an Appendable object to store the output and using the format() method to put formatted content into that object. Here's a list of implementers for the Appendable interface:

BufferedWriter
CharArrayWriter
CharBuffer
FileWriter
FilterWriter
LogStream
OutputStreamWriter
PipedWriter
PrintStream
PrintWriter
StringBuffer
StringBuilder
StringWriter
Writer
An object implementing this interface can be used as the destination when using a Formatter class by passing the object into the Formatter constructor. Most of these classes should look familiar, except for the StringBuilder class. StringBuilder is nearly identical to the StringBuffer class, with one big exception: It isn't thread safe. If you know you are going to build up a string in a single thread, use StringBuilder. If the building can cross thread bounds, use StringBuffer. Listing 1 shows how you'd typically start using Formatter:

Listing 1. Typical formatter usage

StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);



After creating a Formatter class, you call its format() method with format strings and arguments. If you need to use a different Locale than that sent into the constructor for part of the formatted output, you can also pass in a Locale object to the format() method. Listing 2 shows the two varieties of format():

Listing 2. format() methods of Formatter

public Formatter format(String format,
Object... args)
public Formatter format(Locale l,
String format,
Object... args)



If you want to get the value of Pi to 10 digits of precision, the code in Listing 3 will put that value into the StringBuilder and print the output. Printing the formatter object will display the content of the Appendable object.

Listing 3. Demonstrating a Formatter

import java.util.Locale;
import java.util.Formatter;

public class Build {
public static void main(String args[]) {
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
formatter.format("PI = %12.10f", Math.PI);
System.out.println(formatter);
}
}



Don't forget to compile with the -source 1.5 option or the compiler won't recognize the variable argument list. Because formatting output and sending it to the console are common tasks, there are conveniences available to this behavior. We'll look at those next.

PrintStream support
The PrintStream class contains the definition of the common System.out and System.err objects for writing to standard output and standard error, respectively. Introduced in Tiger are two new constructors (for going straight to a file) and six methods for formatting support (three sets of pairs). The first pair is different versions of the append() method. This pair implements the new java.lang.Appendable interface. You would typically not call these methods directly. The ones you would call directly are format() and printf(), where the printf() versions are just convenience wrappers for the format() versions, as shown in Listing 4:

Listing 4. PrintStream.format methods

public PrintStream format(String format,
Object... args)
public PrintStream format(Locale l,
String format,
Object... args)



Keep in mind the new variable argument support, which is designated by the ... shown in Listing 4 above.

Listing 5 demonstrates the use of the format() method of PrintStream to print today's date:

Listing 5. Example PrintStream.format usage
/
import java.util.Calendar;

public class Now {
public static void main(String args[]) {
System.out.format(
"Today is %1$tB %1$te, %1$tY.",
Calendar.getInstance()
);
}
}



The output from running this program is Today is April 2, 2004., although the actual output depends on the date you run the program. The %1$tB formatting string in the code above tells the program to use the first argument and print out the full month name for the date object. The %1$te formatting string means to display the day of the month and the %1$tY formatting string is for the four-digit year. Other options for printing dates and times are shown in the Javadoc for the Formatter object.

String support
The String class has two new static format() methods that work similarly to their printf() equivalents. Send a format string and arguments (with a possible Locale) and use what is specified in the format string to convert the arguments. In the case of the String version of the method, the results are sent back as a String object, instead of going through the stream. These methods aren't overly spectacular, but they allow you to avoid using the Formatter object directly and creating an intermediate StringBuilder.

Formatting arbitrary objects
Everything you've seen so far describes how to use the new formatting capabilities to format existing objects and primitive types. If you want to provide support for using your own objects with Formatter, that's where the Formattable interface comes into play. By implementing the single formatTo() method shown in Listing 6 in your own class, you can use your own classes with format strings:

Listing 6. Formattable interface

void formatTo(Formatter formatter,
int flags,
Integer width,
Integer precision)



Listing 7 demonstrates the use of the Formattable interface by providing a simple class with a name property. That name is displayed in the output, with support for controlling the width of the output and the justification.

Listing 7. Example formattable usage

import java.util.Locale;
import java.util.Formatter;
import java.util.Formattable;

public class MyObject implements Formattable {
String name;
public MyObject(String name) {
this.name = name;
}
public void formatTo(
Formatter fmt,
int f,
Integer width,
Integer precision) {

StringBuilder sb = new StringBuilder();
if (precision == null) {
// no max width
sb.append(name);
} else if (name.length() < precision) {
sb.append(name);
} else {
sb.append(name.substring(0, precision - 1)).append('*');
}

// apply width and justification
if ((width != null) && (sb.length() < width)) {
for (int i = 0, n=sb.length(); i < width - n; i++) {
if ((f & Formattable.LEFT_JUSTIFY) == Formattable.LEFT_JUSTIFY) {
sb.append(' ');
} else {
sb.insert(0, ' ');
}
}
}
fmt.format(sb.toString());
}

public static void main(String args[]) {
MyObject my1 = new MyObject("John");
MyObject my2 = new MyObject("Really Long Name");
// First / Using toString()
System.out.println("First Object : " + my1);
// Second / Using Formatter
System.out.format("First Object : '%s'\n", my1);
// Second / Using Formatter
System.out.format("Second Object: '%s'\n", my2);
// Second / Using Formatter with width
System.out.format("Second Object: '%10.5s'\n", my2);
// Second / Using Formatter with width and left justification
System.out.format("Second Object: '%-10.5s'\n", my2);
}
}



Running this program produces the output in Listing 8. The first two lines demonstrate the difference between using toString and Formatter. The last three show options for width and justification control.

Listing 9. Example formattable output

First Object : MyObject@10b62c9
First Object : 'John'
Second Object: 'Really Long Name'
Second Object: ' Real*'
Second Object: 'Real* '



Conclusion
Getting a grasp of all the formatting options available with Formatter will take a little time, unless you are familiar with them from the C world. Some minor differences exist, but for the most part the behaviors are very similar. One key difference with the Java platform is that when the formatting string is invalid, an exception will be thrown.

Be sure to take a long look at the formatting strings available, as shown in the Formatter class Javadoc. When creating your own custom classes, not only should you provide a toString() implementation, but implementing the Formattable interface will typically be beneficial.

Tuesday, October 05, 2004

The GregorianCalendar class

One way to create an object representing an arbitrary date is to use the following constructor of the GregorianCalendar class, found in the java.util package:


GregorianCalendar(int year, int month, int date)


Note that for the month, January is 0, February is 1, and so on, until December, which is 11. Since those are not the numbers most of us associate with the months of the year, programs will probably be more readable if they use the constants of the parent Calendar class: JANUARY, FEBRUARY, and so on. So, to create an object representing the date that Wilbur and Orville Wright first flew their motored aircraft (December 17, 1903), you can use:



GregorianCalendar firstFlight = new GregorianCalendar(1903, Calendar.DECEMBER, 17);


For clarity's sake, you should use the preceding form. However, you should also learn how to read the shorter form, below. The following example represents the same December 17, 1903, date (remember, in the shorter form 11 represents December):



GregorianCalendar firstFlight = new GregorianCalendar(1903, 11, 17);


In the previous section, you learned how to turn Date objects into Strings. You will do the same again; but first, you need to convert a GregorianCalendar object to a Date. To do so, you will use the getTime() method, which GregorianCalendar inherits from its parent Calendar class. The getTime() method returns a Date corresponding to a GregorianCalendar object. You can put the whole process of creating a GregorianCalendar object, converting it to a Date, and getting and outputting the corresponding String in the following program:



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

public class Flight {

public static void main(String[] args) {
GregorianCalendar firstFlight = new GregorianCalendar(1903, Calendar.DECEMBER, 17);
Date d = firstFlight.getTime();
DateFormat df = DateFormat.getDateInstance();
String s = df.format(d);
System.out.println("First flight was " + s);
}
}


Sometimes it is useful to create an instance of the GregorianCalendar class representing the day the instance was created. To do so, simply use the GregorianCalendar constructor taking no arguments, such as:



GregorianCalendar thisday = new GregorianCalendar();


A sample program to output today's date, starting with a GregorianCalendar object is:



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

class Today {
public static void main(String[] args) {
GregorianCalendar thisday = new GregorianCalendar(); Date d = thisday.getTime();
DateFormat df = DateFormat.getDateInstance();
String s = df.format(d);
System.out.println("Today is " + s);
}
}


Note the similarities between the Date() constructor and the GregorianCalendar() constructor: both create an object, which in simple terms, represents today.

Date manipulation
The GregorianCalendar class offers methods for manipulating dates. One useful method is add(). With the add() method, you can add such time units as years, months, and days to a date. To use the add() method, you must supply the field being increased, and the integer amount by which it will increase. Some useful constants for the fields are DATE, MONTH, YEAR, and WEEK_OF_YEAR. The add() method is used in the program below to calculate a date 80 days in the future. Phileas Fogg, the central character in Jules Verne's Around the World in 80 Days, could have used such a program to calculate a date 80 days from his departure on October 2, 1872:



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

public class World {
public static void main(String[] args) {
GregorianCalendar worldTour = new GregorianCalendar(1872, Calendar.OCTOBER, 2);
worldTour.add(GregorianCalendar.DATE, 80);
Date d = worldTour.getTime();
DateFormat df = DateFormat.getDateInstance();
String s = df.format(d);
System.out.println("80 day trip will end " + s);
}
}


While the example is a bit fanciful, adding days to a date is a common operation: video rentals can be due in 3 days, a library may lend books for 21 days, stores frequently require purchased items to be exchanged within 30 days. The following program shows a calculation using years:



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

public class Mortgage {
public static void main(String[] args) {
GregorianCalendar mortgage = new GregorianCalendar(1997, Calendar.MAY, 18);
mortgage.add(Calendar.YEAR, 15);
Date d = mortgage.getTime();
DateFormat df = DateFormat.getDateInstance();
String s = df.format(d);
System.out.println("15 year mortgage amortized on " + s); }
}


One important side effect of the add() method is that it changes the original date. Sometimes it is important to have both the original date and the modified date. Unfortunately, you cannot simply create a new GregorianCalendar object set equal to the original. The reason is that the two variables have a reference to one date. If the date is changed, both variables now refer to the changed date. Instead, a new object should be created. The following example will demonstrate this:



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

public class ThreeDates {
public static void main(String[] args) {
GregorianCalendar gc1 = new GregorianCalendar(2000, Calendar.JANUARY, 1);
GregorianCalendar gc2 = gc1;
GregorianCalendar gc3 = new GregorianCalendar(2000, Calendar.JANUARY, 1);
//Three dates all equal to January 1, 2000

gc1.add(Calendar.YEAR, 1);
//gc1 and gc2 are changed

DateFormat df = DateFormat.getDateInstance();

Date d1 = gc1.getTime();
Date d2 = gc2.getTime();
Date d3 = gc3.getTime();

String s1 = df.format(d1);
String s2 = df.format(d2);
String s3 = df.format(d3);

System.out.println("gc1 is " + s1);
System.out.println("gc2 is " + s2);
System.out.println("gc3 is " + s3);
}
}


After the program is run, gc1 and gc2 are changed to the year 2001 (because both objects are pointing to the same underlying date representation, which has been changed). The object gc3 is pointing to a separate underlying date representation, which has not been changed.

Calculating review dates
At this point, you have a program based on a real-world example. This particular program will calculate the dates for reviewing material. For example, while reading this article, you might come across some points you want to remember. Unless you have photographic memory, you will likely find that periodically reviewing the new material will aid in remembering it. One reviewing system, discussed in Kurt Hanks and Gerreld L. Pulsipher's Five Secrets to Personal Productivity, suggests briefly covering the material immediately after it is first seen, then after one day, one week, one month, three months, and one year. For this article, you would give it a quick review immediately, another tomorrow, then one week, one month, three months, and one year from now. Our program will calculate the dates.

To be most useful, the program would be incorporated in PIM (Personal Information Manager) software, which would run the program and schedule the reviews. The getDates() method in the following ReviewDates program would be useful for integrating with electronic software as it returns an array of dates (the review dates). Additionally, you can return individual dates using the methods getFirstDay(), getOneDay(), getOneWeek(), getOneMonth(), and getOneYear(). While it is beyond the scope of this article to integrate the ReviewDates class with a PIM, the ReviewDates class does show how to calculate dates based on elapsed time. Now you can easily modify it for other operations that require elapsed time, such as library loans, tape rentals, and mortgage calculations. First, the ReviewDates class is shown below:



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

public class ReviewDates {
private GregorianCalendar firstDay, oneDay, oneWeek, oneMonth, oneQuarter, oneYear;
final int dateArraySize = 6;

ReviewDates(GregorianCalendar gcDate) {
int year = gcDate.get(GregorianCalendar.YEAR);
int month = gcDate.get(GregorianCalendar.MONTH);
int date = gcDate.get(GregorianCalendar.DATE);

firstDay = new GregorianCalendar(year, month, date);
oneDay = new GregorianCalendar(year, month, date);
oneWeek = new GregorianCalendar(year, month, date);
oneMonth = new GregorianCalendar(year, month, date);
oneQuarter = new GregorianCalendar(year, month, date);
oneYear = new GregorianCalendar(year, month, date);

oneDay.add(GregorianCalendar.DATE, 1);
oneWeek.add(GregorianCalendar.DATE, 7);
oneMonth.add(GregorianCalendar.MONTH, 1);
oneQuarter.add(GregorianCalendar.MONTH, 3);
oneYear.add(GregorianCalendar.YEAR, 1);
}

ReviewDates() {
this(new GregorianCalendar());
}

public void listDates() {
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
Date startDate = firstDay.getTime();
Date date1 = oneDay.getTime();
Date date2 = oneWeek.getTime();
Date date3 = oneMonth.getTime();
Date date4 = oneQuarter.getTime();
Date date5 = oneYear.getTime();

String ss = df.format(startDate);
String ss1 = df.format(date1);
String ss2 = df.format(date2);
String ss3 = df.format(date3);
String ss4 = df.format(date4);
String ss5 = df.format(date5);

System.out.println("Start date is " + ss);
System.out.println("Following review dates are:");
System.out.println(ss1);
System.out.println(ss2);
System.out.println(ss3);
System.out.println(ss4);
System.out.println(ss5);
System.out.println();
}

public GregorianCalendar[] getDates() {
GregorianCalendar[] memoryDates = new GregorianCalendar[dateArraySize];
memoryDates[0] = firstDay;
memoryDates[1] = oneDay;
memoryDates[2] = oneWeek;
memoryDates[3] = oneMonth;
memoryDates[4] = oneQuarter;
memoryDates[5] = oneYear;
return memoryDates;
}

public GregorianCalendar getFirstDay() {
return this.firstDay;
}

public GregorianCalendar getOneDay() {
return this.oneDay;
}

public GregorianCalendar getOneWeek() {
return this.oneWeek;
}

public GregorianCalendar getOneMonth() {
return this.oneMonth;
}

public GregorianCalendar getOneQuarter() {
return this.oneQuarter;
}

public GregorianCalendar getOneYear() {
return this.oneYear;
}
}


An example of a program that uses the ReviewDates class to make a simple listing of review dates is shown below:



import java.util.*;

public class ShowDates {
public static void main(String[] args) {
ReviewDates rd = new ReviewDates();
rd.listDates();

GregorianCalendar gc = new GregorianCalendar(2001, Calendar.JANUARY, 15);
ReviewDates jan15 = new ReviewDates(gc);
jan15.listDates();
}
}



Conclusion
This article has introduced three important classes for working with dates: Date, DateFormat, and GregorianCalendar. These classes let you create dates, change them into Strings, and make elementary calculations with dates. In terms of working with dates in Java, this article has only scratched the surface. However, the classes and methods that I have introduced here not only serve as springboard for more advanced learning, but in themselves can also handle many common date-related tasks.

Calculating Java dates

To keep track of time, Java counts the number of milliseconds from the start of January 1, 1970. This means, for example, that January 2, 1970, began 86,400,000 milliseconds later. Similarly, December 31, 1969, began 86,400,000 milliseconds before January 1, 1970. The Java Date class keeps track of those milliseconds as a long value. Because long is a signed number, dates can be expressed before and after the start of January 1, 1970. The largest positive and negative values expressible by the long primitive can generate dates forward and backward about 290,000,000 years, which suits most people's schedules.

The Date class
The Date class, found in the java.util package, encapsulates a long value representing a specific moment in time. One useful constructor is Date(), which creates a Date object representing the time the object was created. The getTime() method returns the long value of a Date object. In the program below, I use the Date() constructor to create a date based on when the program was run, and the getTime() method to find out the number of milliseconds that the date represents:



import java.util.*;


public class Now {
public static void main(String[] args) {
Date now = new Date();
long nowLong = now.getTime();
System.out.println("Value is " + nowLong);
}
}


When I ran that program, it gave me a value of 972,568,255,150. A quick check with my calculator confirms this number is at least in the correct ballpark: it's a bit less than 31 years, which corresponds to the right number of years between January 1, 1970, and the day I wrote this article. While computers may thrive on numbers like the foregoing value, most people are reluctant to say such things as "I'll see you on 996,321,998,346." Fortunately, Java provides a way to convert Date objects to Strings, which represent dates in more traditional ways. The DateFormat class, discussed in the next section, can create Strings with alacrity.

The DateFormat class
One purpose of the DateFormat class is to create Strings in ways that humans can easily deal with them. However, because of language differences, not all people want to see a date in exactly the same way. Someone in France may prefer to see "25 decembre 2000," while someone in the United States may be more accustomed to seeing "December 25, 2000." So when an instance of a DateFormat class is created, the object contains information concerning the particular format in which the date is to be displayed. To use the default format of the user's computer, you can apply the getDateInstance method in the following way to create the appropriate DateFormat object:


DateFormat df = DateFormat.getDateInstance();


The DateFormat class is found in the java.text package.

Converting to a String
You can convert a Date object to a string with the format method. This is shown in the following demonstration program:



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

public class NowString {
public static void main(String[] args) {
Date now = new Date();
DateFormat df = DateFormat.getDateInstance();
String s = df.format(now);
System.out.println("Today is " + s);
}
}


The getDateInstance method shown in the code above, with no arguments, creates an object in the default format or style. Java also provides some alternative styles for dates, which you can obtain through the overloaded getDateInstance(int style). For convenience' sake, DateFormat provides some ready-made constants that you can use as arguments in the getDateInstance method. Some examples are SHORT, MEDIUM, LONG, and FULL, which are demonstrated in the program below:



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

public class StyleDemo {
public static void main(String[] args) {
Date now = new Date();

DateFormat df = DateFormat.getDateInstance();
DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat df2 = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
DateFormat df4 = DateFormat.getDateInstance(DateFormat.FULL);
String s = df.format(now);
String s1 = df1.format(now);
String s2 = df2.format(now);
String s3 = df3.format(now);
String s4 = df4.format(now);

System.out.println("(Default) Today is " + s);
System.out.println("(SHORT) Today is " + s1);
System.out.println("(MEDIUM) Today is " + s2);
System.out.println("(LONG) Today is " + s3);
System.out.println("(FULL) Today is " + s4);
}
}


That program output the following:



(Default) Today is Nov 8, 2000
(SHORT) Today is 11/8/00
(MEDIUM) Today is Nov 8, 2000
(LONG) Today is November 8, 2000
(FULL) Today is Wednesday, November 8, 2000


The same program, after being run on my computer with the default regional settings changed to Swedish, displayed this output:



(Default) Today is 2000-nov-08
(SHORT) Today is 2000-11-08
(MEDIUM) Today is 2000-nov-08
(LONG) Today is den 8 november 2000
(FULL) Today is den 8 november 2000


From that, you can see that in Swedish the months of the year are not capitalized (although November is still november). Also, note that the LONG and FULL versions are identical in Swedish, while they differ in American English. Additionally, it is interesting that the Swedish word for Wednesday, onsdag, is not included in the FULL version, where the English FULL version includes the name of the day.

Note that you can use the getDateInstance method to change the language for a DateFormat instance; however, in the case above, it was done on a Windows 98 machine by changing the regional settings from the control panel. The lesson here is that the default regional setting varies from place to place, which has both advantages and disadvantages of which the Java programmer should be aware. One advantage is that the Java programmer can write a single line of code to display a date, yet have the date appear in tens or even hundreds of different forms when the program is run on computers throughout the world. But that can be a disadvantage if the programmer wants just one format -- which is preferable, for example, in a program that outputs text and dates mixed together. If the text is in English, it would be inconsistent to have dates in other formats, such as German or Spanish. If the programmer relies on the default regional format, the date format will vary according to the executing computer's regional settings.

Parsing a String
You can also use the DateFormat class to create Date objects from a String, via the parse() method. This particular method can throw a ParseException error, so you must use proper error-handling techniques. A sample program that turns a String into a Date is shown below:



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

public class ParseExample {
public static void main(String[] args) {
String ds = "November 1, 2000";
DateFormat df = DateFormat.getDateInstance();
try {
Date d = df.parse(ds);
}
catch(ParseException e) {
System.out.println("Unable to parse " + ds);
}
}
}


The parse() method is a useful tool for creating arbitrary dates. I will examine another way of creating arbitrary dates. Also, you will see how to do elementary calculations with dates, such as calculating the date 90 days after another date. You can accomplish both tasks with the GregorianCalendar class.

Generating excel file as well as redirecting to a different page

You might be able to accomplish what you want with clever use of javascript on the client.

For example, put the following in a SCRIPT tag:
function launchExcel() {
newWindow = window.open('excelservlet','_blank');
window.location.href='downloadsuccess.jsp';
}

And the link to the Excel file in your JSP should have this attribute: onclick="javascript:launchExcel();"

This will cause the Excel sheet to be loaded by a separate window, and the current window will navigate to the "excel file successfully generated" page.

Monday, October 04, 2004

Java Final Release

Hello Friends ,

Sun Has Finally released Javas lattest Version : J2sdk 5 on 30th of September
Read More About It On The (www.theserverside.com).

Enjoy .......