Java
  Home arrow Java arrow Conversions and Java Print Streams
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Conversions and Java Print Streams
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-06-28

    Table of Contents:
  • Conversions and Java Print Streams
  • Character conversions
  • Format Modifiers
  • Flags
  • Precision

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Conversions and Java Print Streams


    (Page 1 of 5 )

    In the conclusion to this three-part article, we'll discuss data and time conversions, character conversions, and more. This article is excerpted from chapter seven of Java I/O, Second Edition, written by Elliotte Rusty Harold (O'Reilly, 2006; ISBN: 0596527500). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    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 7-3 prints the current date and time in all of these formats.

    Example 7-3.  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

    More Java Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Java I/O, Second Edition," published by...
     

    Buy this book now. This article is excerpted from chapter seven of Java I/O, Second Edition, written by Elliotte Rusty Harold (O'Reilly, 2006; ISBN: 0596527500). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek