Demonstrate number and date internationalization : Date Format « I18N « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » I18N » Date FormatScreenshots 
Demonstrate number and date internationalization
Demonstrate number and date internationalization
 
/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;

/**
 * A partial implementation of a hypothetical stock portfolio class. We use it
 * only to demonstrate number and date internationalization.
 */
public class Portfolio {
  EquityPosition[] positions;

  Date lastQuoteTime = new Date();

  public Portfolio(EquityPosition[] positions, Date lastQuoteTime) {
    this.positions = positions;
    this.lastQuoteTime = lastQuoteTime;
  }

  public void print(PrintWriter out) {
    // Obtain NumberFormat and DateFormat objects to format our data.
    NumberFormat number = NumberFormat.getInstance();
    NumberFormat price = NumberFormat.getCurrencyInstance();
    NumberFormat percent = NumberFormat.getPercentInstance();
    DateFormat shortdate = DateFormat.getDateInstance(DateFormat.MEDIUM);
    DateFormat fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG,
        DateFormat.LONG);

    // Print some introductory data.
    out.println("Portfolio value at " + fulldate.format(lastQuoteTime)
        ":");
    out.println("Symbol\tShares\tPurchased\tAt\t" "Quote\tChange");

    // Display the table using the format() methods of the Format objects.
    for (int i = 0; i < positions.length; i++) {
      out.print(positions[i].name + "\t");
      out.print(number.format(positions[i].shares"\t");
      out.print(shortdate.format(positions[i].purchased"\t");
      out.print(price.format(positions[i].bought"\t");
      out.print(price.format(positions[i].current"\t");
      double change = (positions[i].current - positions[i].bought)
          / positions[i].bought;
      out.println(percent.format(change));
      out.flush();
    }
  }

  static class EquityPosition {
    String name; // Name of the stock.

    int shares; // Number of shares held.

    Date purchased; // When purchased.

    double bought; // Purchase price per share

    double current; // Current price per share

    EquityPosition(String n, int s, Date when, double then, double now) {
      name = n;
      shares = s;
      purchased = when;
      bought = then;
      current = now;
    }
  }

  /**
   * This is a test program that demonstrates the class
   */
  public static void main(String[] args) {
    // This is the portfolio to display. Note we use a deprecated
    // Date() constructor here for convenience. It represents the year
    // offset from 1900, and will cause a warning message when compiling.
    EquityPosition[] positions = new EquityPosition[] {
        new EquityPosition("XXX"400new Date(10013)11.90,
            13.00),
        new EquityPosition("YYY"1100new Date(10022)71.09,
            27.25),
        new EquityPosition("ZZZ"6000new Date(100417)23.37,
            89.12) };

    // Create the portfolio from these positions
    Portfolio portfolio = new Portfolio(positions, new Date());

    // Set the default locale using the language code and country code
    // specified on the command line.
    if (args.length == 2)
      Locale.setDefault(new Locale(args[0], args[1]));

    // Now print the portfolio
    portfolio.print(new PrintWriter(System.out));
  }
}

           
         
  
Related examples in the same category
1. Show DateShow Date
2. Simple Date formatSimple Date format
3. Java I18N: Format : Date FormatJava I18N: Format : Date Format
4. Java I18N: Format : Date Format Symbols DemoJava I18N: Format : Date Format Symbols Demo
5. Use some locales choicesUse some locales choices
6. Format some values using the default locale
7. Formatting and Parsing a Time for a Locale Using Default Formats
8. Formatting and Parsing a Time for a Locale: specify the locale when creating the SimpleDateFormat object.
9. Formatting and Parsing a Date for a Locale: specify the locale when creating the SimpleDateFormat object.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.