(1) Date and time conversions.....
Date and time conversions can be applied to java.util.Calendar and java.util.Date objects. They can also be applied to long andLongvalues, in which case the value is assumed to be the number of milliseconds since midnight, January 1, 1970. All date and time conversions begin with atfor lowercase or aTfor uppercase. These conversions are:
%tH/ %TH
Two-digit hour using a 24-hour clock, ranging from 00
to 23
%tI/%TI
Two-digit hour using a 12-hour clock, ranging from 01
to 12
%tk/ %Tk
One- or two-digit hour using a 24-hour clock, ranging
from 0 to 23
%tl/ %Tl
One- or two-digit hour using a 12-hour clock, ranging
from 1 to 12
%tM/ %TM
Two-digit minutes, ranging from 00 to 59
%tS/ %TS
Two-digit seconds, ranging from 00 to 60 (60 is used
for leap seconds)
%tL/ %TL
Three-digit milliseconds, ranging from 000 to 999
%tN/ %TN
Nine-digit nanoseconds, ranging from 000000000 to
999999999
%tp/ %Tp
Locale-specific morning/afternoon indicator, such as
am or PM
%tz/ %Tz
RFC 822 numeric time zone indicator as an offset
from UMT (for instance, Eastern Standard Time is–
0500)
%tZ/ %TZ
An abbreviation for the time zone, such as edt or EST
%ts/ %Ts
Seconds elapsed since midnight, January 1, 1970,
Greenwich Mean Time
%TQ
Milliseconds elapsed since midnight, January 1, 1970,
Greenwich Mean Time
%tB/ %TB
Localized month, such as “January” or “JANVIER”
%tb/ %Tb
Localized, abbreviated month, such as “Jan” or “JAN”
%th/ %Th
Localized, abbreviated month, such as “Jan” or
“JAN” (yes,%tband%thare synonyms; I have no
idea why)
%tA/ %TA
Localized day name, such as “Tuesday” or “MARDI”
%ta/ %Ta
Localized, abbreviated day, such as “Tue” or “TUE”
%tC/ %TC
Two-digit century, ranging from 00 to 99
%tY/ %TY
Year with at least four digits, ranging from 0001 to
the indefinite future
%ty/ %Ty
Two-digit year, ranging from 00 to 99
%tj/ %Tj
Three-digit day of the year, ranging from 001 to 366
%tm/ %Tm
Two-digit month, ranging from 01 to 13 (13 is used in
some non-Gregorian lunar calendars)
%td/ %Td
Two-digit day of the month, ranging from 01 to 31
%te/ %Te
One- or two-digit day of the month, ranging from 1 to
31
%tR/ %TR
Hours and minutes on a 24-hour clock, such as 03:23
or 14:07
%tT/ %TT
Hours, minutes, and seconds on a 24-hour clock,
such as 03:23:17 or 14:07:00
%tr/ %Tr
Hours, minutes, and seconds on a 12-hour clock,
such as 03:23:17 am or 02:07:00 PM
%tD/ %TD
Date in the form month/day/year, such as 05/12/06
%tF/ %TF
ISO 8601 standard date in the form year-month-day,
such as 2006-05-12
%tc/ %Tc
Date and time formatted like so: “Fri May 12
12:27:30 EDT 2006”
Example :- prints the current date and time in all of these formats.
Example :- Date format specifiers
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date now = new Date();
System.out.printf("two-digit hour on a 24-hour clock: %tH/%TH\n", now, now);
System.out.printf("two-digit hour on a 12-hour clock: %tI/%TI\n", now, now);
System.out.printf("one- or two-digit hour on a 24-hour clock: %tk/%Tk\n",
now, now);
System.out.printf("one- or two-digit hour on a 12-hour clock: %tl/%Tl\n", now,
now);
System.out.printf("two-digit minutes ranging from 00 to 59: %tH/%TH\n",
now, now);
System.out.printf("two-digit seconds ranging from 00 to 60 : %tS/%TS\n",
now, now);
System.out.printf("milliseconds: %tL/%TL\n", now, now);
System.out.printf("nanoseconds: %tN/%TN\n", now, now);
System.out.printf("locale-specific morning/afternoon indicator: %tp/%Tp\n",
now, now);
System.out.printf("RFC 822 numeric time zone indicator: %tz/%Tz\n", now, now);
System.out.printf("time zone abbreviation: %tZ/%TZ\n", now, now);
System.out.printf("seconds since the epoch: %ts/%Ts\n", now, now);
System.out.printf("milliseconds since the epoch: %TQ\n", now);
System.out.printf("localized month name: %tB/%TB\n", now, now);
System.out.printf("localized, abbreviated month: %tb/%Tb\n", now, now);
System.out.printf("localized, abbreviated month: %th/%Th\n", now, now);
System.out.printf("localized day name: %tA/%TA\n", now, now);
System.out.printf("localized, abbreviated day: %ta/%Ta\n", now, now);
System.out.printf("two-digit century:
%tC/%TC\n", now, now);
System.out.printf("four-digit year:
%tY/%TY\n", now, now);
System.out.printf("two-digit year:
%ty/%Ty\n", now, now);
System.out.printf("three-digit day of the year: %tj/%Tj\n", now, now);
System.out.printf("two-digit month:
%tm/%Tm\n", now, now);
System.out.printf("two-digit day of the month: %td/%Td\n", now, now);
System.out.printf("one- or two-digit day of the month: %te/%Te\n", now, now);
System.out.printf("hours and minutes on a 24-hour clock: %tR/%TR\n", now, now);
System.out.printf("hours, minutes, and seconds on a 24-hour clock: %tT/%TT\n",
now, now);
System.out.printf("hours, minutes, and seconds on a 12-hour clock: %tr/%Tr\n",
now, now);
System.out.printf("month/day/year:
%tD/%TD\n", now, now);
System.out.printf("ISO 8601 standard date: %tF/%TF\n", now, now);
System.out.printf("Unix date format:
%tc/%Tc\n", now, now);
}
}
Here’s the output when this was run on Friday, June 24, 2005 at 6:43 PM EDT:
two-digit hour on a 24-hour clock: 18/18
two-digit hour on a 12-hour clock: 06/06
one- or two-digit hour on a 24-hour clock: 18/18
one- or two-digit hour on a 12-hour clock: 6/6
two-digit minutes ranging from 00 to 59: 18/18
two-digit seconds ranging from 00 to 60 : 50/50
milliseconds: 859/859
nanoseconds: 859000000/859000000
locale-specific morning/afternoon indicator: pm/PM
RFC 822 numeric time zone indicator:
-0500/-0500
time zone abbreviation: EDT/EDT
seconds since the epoch: 1119653030/1119653030
milliseconds since the epoch: 1119653030859
localized month name: June/JUNE
localized, abbreviated month: Jun/JUN
localized, abbreviated month: Jun/JUN
localized day name: Friday/FRIDAY
localized, abbreviated day: Fri/FRI
two-digit century: 20/20
four-digit year: 2005/2005
two-digit year: 05/05
three-digit day of the year: 175/175
two-digit month: 06/06
two-digit day of the month: 24/24
one- or two-digit day of the month: 24/24
hours and minutes on a 24-hour clock: 18:43/18:43
hours, minutes, and seconds on a 24-hour clock: 18:43:50/18:43:50
hours, minutes, and seconds on a 12-hour clock: 06:43:50 PM/06:43:50 PM
month/day/year: 06/24/05/06/24/05
ISO 8601 standard date: 2005-06-24/2005-06-24
Unix date format: Fri Jun 24 18:43:50 EDT 2005/FRI JUN 24 18:43:50 EDT 2005
(2) Conversions and Java Print Streams - Character conversions
Character conversions can be applied to char and java.lang.Character objects. They can also be applied to byte, short,int, and the equivalent type-wrapper objects if the integer falls into the range of Unicode code points (0 to 0x10FFFF). These
conversions are:
%c
A lowercase Unicode character
%C
An uppercase Unicode character
Boolean conversions
Boolean conversions can be applied to boolean primitive values and java.lang.Boolean objects. They can also be applied to all object types, in which case they’re considered to be true if the object is nonnull and false if it is null. All other primitive types are considered to be true, regardless of value. These conversions are:
%b
“true” or “false”
%B
“TRUE” or “FALSE”
These conversions are not localized. Even in France, you’ll see “true” and “false” instead of “vrai” and “faux.”
General conversions
There are two more conversions that can be applied to any object and also to primitive types after autoboxing. These are:
%h/%H
The lowercase/uppercase hexadecimal form of the
object’shashCode, or “null” or “NULL” if the object is
null.
%s/%S
The result of invoking the object’sformatTo()
method if it implements Formattable; otherwise,
the result of invoking its toString()method, or
“null” if the object is null. With%S, this value is then
converted to uppercase.
Example :- General format specifiers
import java.net.*;
public class GeneralFormatExample {
public static void main(String[] args) throws MalformedURLException{
URL u = new URL(http://www.example.com/Article.html);
System.out.printf("boolean: %b\n", u);
System.out.printf("BOOLEAN: %B\n", u);
System.out.printf("hashcode: %h\n", u);
System.out.printf("HASHCODE: %H\n", u);
System.out.printf("string: %s\n", u);
System.out.printf("STRING: %S\n", u);
}
}
Here’s the output from running this on a U.S.-localized system:
boolean: true
BOOLEAN: TRUE
hashcode: 79d2cef0
HASHCODE: 79D2CEF0
string: http://www.example.com/ Article.html
STRING: HTTP://WWW.EXAMPLE.COM/ ARTICLE.HTML
(3). Conversions and Java Print Streams - Format Modifiers
In addition to a conversion code, the format string can also specify a width, a precision, the argument it’s replaced with, and any of several special-purpose flags. The most general format follows this pattern:
%[argument_index$][flags][width][.precision]conversion
Here is a quick definition of those parameters. More detail on each will follow:
argument_index
The number of the argument with which to replace this tag
flags
Indicators of various formatting options
width
The minimum number of characters with which to format the replacement value
precision
The number of characters after the decimal point; alternately, the maximum number of characters in the formatted string
These four options control exactly how a string is formatted as tags are replaced by values.
Argument index
The argument index is specified when the order of the arguments does not match the order of the tags. For example:
out.printf("There are %2$f centimeters in %1$f feet.", feet, 2.54 * feet * 12);
In this case, indexes start with 1 rather than 0, which is unusual for Java. (The format string counts as argument 0.) If you reference a nonexistent argument,printf()throws aMissingFormatArgumentException.
The argument index is particularly useful when you want to repeat the same value more than once in a string, perhaps formatted differently each time. For example:
System.out.printf("Hexadecimal: %1$H Decimal: %1$f", Math.PI);
You can also repeat the previous argument by specifying a less than sigh (<) rather than an integer and a dollar sign ($). For instance, this statement is equivalent to the previous statement:
System.out.printf("Hexadecimal: %1$H Decimal: %
(4). Conversions and Java Print Streams - Flags
Flags indicate a variety of formatting options. Not all flags apply to all types. Using a flag to format a type that does not apply throws a FormatFlagsConversionMismatchException, a subclass of IllegalFormatException. However, you can combine multiple flags that do apply.
Flag Signifies Applies to
- Left-justify. All
# Alternate form. General, integer, floating point
+ Include a sign even if positive.
(Normally, only negative numbers have signs.) Integer, floating point
space Add a leading space to positive numbers. Integer, floating point
(This is where the sign would be and helps line up
positive and negative numbers.)
0 Pad with zeros instead of spaces. Integer, floating point
, Use the locale-specific grouping separator instead Integer, floating point
of a period.
( Use instead of a minus sign to indicate negative Integer, floating point
numbers.
For example, this statement prints adoubleformatted as a 20-character decimal padded with zeros, using a locale-specific grouping separator and parentheses for negative numbers:
System.out.printf("%(+0,20f", -Math.PI);
The result is(00000000003.141593).
The relative order of the flags does not matter. This statement prints the same thing:
System.out.printf("%,0+(20f", -Math.PI);
Width
Each of the various conversions may be preceded by an optional width. This sets the minimum number of characters to print. For example, if you format an integer using the code %5d, it will always be printed at least five characters wide. If the integer has fewer than five digits, extra spaces are added on the left-hand side to make up five characters. If it has five or more digits, no extra spaces are added.
The entire number is always printed. If the argument is larger than can fit in five places, all of it will be printed anyway, and subsequent columns may no longer line up properly.
For example, this statement prints five mathematical constants, each 12 places wide:
System.out.printf("%12f %12f %12f %12f
%12f",
Math.PI, Math.E, 1.0, 0.0, Math.sqrt(2));
By default, extra places are filled with space characters to right-justify the numbers. However, flags can be used to fill extra places with zeros or to left-justify the numbers instead.
This is the output:
3.141593 2.718282 1.000000 0.000000 1.414214
Width is not limited to numeric types. You can specify a width for any format tag: date, time, boolean, and so on.
(5). Conversions and Java Print Streams - Precision
Floating-point types (%e, %E, %f, %g, and%G) may also specify a precision in the form.3or.5. The precision comes after the width but before the conversion code. This indicates how many places are used after the decimal point. For example, this statement formats the same five constants 15 places wide, with 3 places after the decimal point:
System.out.printf("%15.3f %15.3f %15.3f
%15.3f %15.3f",
Math.PI, Math.E, 1.0, 0.0, Math.sqrt(2));
This is the output:
3.142 2.718 1.000 0.000 1.414
A precision can also be applied to strings and other nonnumeric, nondate types. In these cases, it specifies the maximum number of characters to write to the output.
Precision cannot be set for integral types, however. Attempting to do so throws anIllegalFormatPrecisionException. As usual, this is a subclass ofIllegalFormatExceptionand a runtime exception.
Formattable:-
You can format your own custom classes by implementing the Formattable interface. In format strings, you use %s and %S to indicate where you want your custom class instances placed. The default formatting for objects matched to %s and %S is simply whatever is returned by toString(). However, more often than not, this is just a debugging string not really meant for display to end users. If your class implements the java.util.Formattable interface, Java will use the return value of the object’s formatTo() method instead. That method has this signature:
public void formatTo(Formatter formatter, int flags, int width, int precision)
The four arguments are:
formatter
TheFormatterthat calledformatTo. More importantly, this is the object onto which the output will be written. Your method will invoke this object’sformat() methods to write to the underlying stream.
flags
A bitmasked constant providing the values of various flags set for this operation: ^,-,#, etc. You interpret these with theFormattableFlagsclass.
width
The minimum number of characters your method must return. If the returned value has fewer characters than the specified minimum, it will be padded with spaces.
precision
The maximum number of characters your method should return. Earlier, I complained that the uppercasing in the URL class was too naïve because, when formatted, it changed the case of case-sensitive parts such as the path and the query string as well as case-insensitive parts such as the scheme and the hostname. There’s another problem with the naïve uppercasing: the scheme and host names are defined in ASCII, but uppercasing isn’t always. In particular, uppercasing the letter i in Turkey produces the capital dotted I rather than the usual undotted capital I. For instance, WWW.microsoft.com uppercases as WWW.MICROSOFT.COM, which will not resolve.
Example:- demonstrates aFormattableURLclass that uppercases only those parts of a URL that can be uppercased without changing its meaning. Ideally this would be a subclass of java.net.URL, but sinceURL is final, delegation is used instead. In essence,FormattableURLis a wrapper around a URL object that just provides theformatTo()method.
Example . Implementing Formattable
import java.util.*;
import java.net.*;
public class FormattableURL implements Formattable {
private URL delegate;
public FormattableURL(URL url){
this.delegate = url;
}
public void formatTo(Formatter formatter, int flags, int width,
int precision) {
if (precision < -1) {
throw new IllegalFormatPrecisionException(precision);
}
if (width < -1) {
throw new IllegalFormatWidthException(width);
}
if (precision > width) {
throw new IllegalFormatPrecisionException(precision);
}
// Check to see if we've been asked to use any flags we don't interpret
int recognizedFlags
= FormattableFlags.UPPERCASE | FormattableFlags.LEFT_JUSTIFY;
boolean unsupportedFlags = ((~recognizedFlags) & flags) != 0;
if (unsupportedFlags) {
// We should really pass the flags to this constructor.
// However, Java doesn't offer any reasonable way to get these.
throw new IllegalFormatFlagsException("#");
}
boolean upperCase = (flags & FormattableFlags.UPPERCASE) != 0;
StringBuffer sb = new StringBuffer();
String scheme = delegate.getProtocol();
if (upperCase && scheme != null) {
scheme = scheme.toUpperCase(Locale.ENGLISH);
}
String hostname = delegate.getHost();
if (upperCase && hostname != null) {
hostname = hostname.toUpperCase(Locale.ENGLISH);
}
String userInfo = delegate.getUserInfo();
int port = delegate.getPort();
String path = delegate.getPath();
String query = delegate.getQuery(); String fragment = delegate.getRef();
if (scheme != null) {
sb.append(scheme);
sb.append("://");
}
if (userInfo != null) {
sb.append(userInfo);
sb.append("@");
}
if (hostname != null) {
sb.append(hostname);
}
if (port != -1) {
sb.append(':');
sb.append(port);
}
if (path != null) {
sb.append(path);
}
if (query != null) {
sb.append('?');
sb.append(query);
}
if (fragment != null) {
sb.append('#');
sb.append(fragment);
}
boolean leftJustify = (flags & FormattableFlags.LEFT_JUSTIFY) != 0;
// Truncate on the right if necessary
if (precision <>
sb.setLength(precision);
}
else {// Pad with spaces if necessary
while (sb.length() <>
if (leftJustify) sb.append(' ');
else sb.insert(0, ' ');
}
}
formatter.format(sb.toString());
}
}
The formatTo()method first checks to see if the values passed make sense—that is, that the width is greater than or equal to the precision, and both are greater than or equal to –1. (–1 simply indicates that these values weren’t set.) Assuming these checks pass, it splits the delegate URL into its component parts and uppercases the two case-insensitive parts (the scheme and the hostname) if the uppercase flag is set. It then appends all the other parts without changing their cases at all. Finally, if the precision is less than the string’s length, the formatted string is truncated on the right. If the string’s length is less than the specified width, the string is padded with spaces: on the right by default but on the left if the left-justified flag is set. If any other flags are present, anIllegalFormatFlagsExceptionis thrown. Thus, it becomes possible to format a URL like this:
URL url = new URL(http://www.example.org/index.html?name=value#Fred);
System.out.printf("%60.40S\n", new FormattableURL(url));

No comments:
Post a Comment