Java by API/java.text/DateFormat
Содержание
- 1 DateFormat: format(Date date)
- 2 DateFormat.FULL
- 3 DateFormat: getAvailableLocales()
- 4 DateFormat: getDateInstance(int style,Locale aLocale)
- 5 DateFormat: getDateTimeInstance(int dateStyle, int timeStyle)
- 6 DateFormat: getTimeInstance(int style,Locale aLocale)
- 7 DateFormat.LONG
- 8 DateFormat.MEDIUM
- 9 DateFormat: parse(String dateString)
- 10 DateFormat: setLenient(boolean lenient)
- 11 DateFormat.SHORT
DateFormat: format(Date date)
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/** Trivial class to show use of Date & Calendar objects */
public class Main {
/** Typical main method ("main program") declaration */
public static void main(String[] av) {
Locale l1 = new Locale("en", "US"), l2 = new Locale("es", "ES");
// Create a Date object for May 5, 1986
Calendar c = Calendar.getInstance();
c.set(1986, 04, 05); // May 5, 1986
Date d1 = c.getTime();
// Create a Date object for today.
Date d2 = new Date(); // today
DateFormat df_us = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM, l1), df_sp = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM, l2);
System.out.println("Date d1 for US is " + df_us.format(d1));
System.out.println("Date d1 for Spain is " + df_sp.format(d1));
System.out.println("Date d2 is " + df_us.format(d2));
}
}
DateFormat.FULL
/*
* Output:
Japan: 4/28/06
Korea: Apr 28, 2006
United Kingdom: 28 April 2006
United States: Friday, April 28, 2006
* */
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class MainClass {
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));
}
}
DateFormat: getAvailableLocales()
/* From http://java.sun.ru/docs/books/tutorial/index.html */
/*
* Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for NON-COMMERCIAL purposes and without fee is hereby granted
* provided that this copyright notice appears in all copies. Please refer to
* the file "copyright.html" for further important copyright and licensing
* information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES.
*/
import java.text.SimpleDateFormat;
import java.util.Locale;
public class Main {
static public void main(String[] args) {
Locale list[] = SimpleDateFormat.getAvailableLocales();
for (int i = 0; i < list.length; i++) {
System.out.println(list[i].toString());
}
for (int i = 0; i < list.length; i++) {
System.out.println(list[i].getDisplayName());
}
for (int i = 0; i < list.length; i++) {
System.out.println(list[i].getDisplayName(Locale.FRANCE));
}
}
}
DateFormat: getDateInstance(int style,Locale aLocale)
/*
* Output:
Japan: 4/28/06
Korea: Apr 28, 2006
United Kingdom: 28 April 2006
United States: Friday, April 28, 2006
* */
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class MainClass {
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));
}
}
DateFormat: getDateTimeInstance(int dateStyle, int timeStyle)
/*
* 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.ru/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 Main {
EquityPosition[] positions;
Date lastQuoteTime = new Date();
public Main(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", 400, new Date(100, 1, 3), 11.90,
13.00),
new EquityPosition("YYY", 1100, new Date(100, 2, 2), 71.09,
27.25),
new EquityPosition("ZZZ", 6000, new Date(100, 4, 17), 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));
}
}
DateFormat: getTimeInstance(int style,Locale aLocale)
/*
* Output:
Japan: 11:25 AM
United Kingdom: 11:25:31 PDT
Canada: 11:25:31 o"clock AM PDT
* */
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class MainClass {
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));
}
}
DateFormat.LONG
/*
* Output:
Japan: 4/28/06
Korea: Apr 28, 2006
United Kingdom: 28 April 2006
United States: Friday, April 28, 2006
* */
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class MainClass {
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));
}
}
DateFormat.MEDIUM
/*
* Output:
Japan: 4/28/06
Korea: Apr 28, 2006
United Kingdom: 28 April 2006
United States: Friday, April 28, 2006
* */
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class MainClass {
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));
}
}
DateFormat: parse(String dateString)
/*
Mon Jan 01 00:00:00 PST 2001 is after Sat Jan 01 00:00:00 PST 2000
* */
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
public static void main(String[] a) throws Exception{
DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
Date d1 = df.parse("2001-01-01");
Date d2 = df.parse("2000-01-01");
String relation;
if (d1.equals(d2))
relation = "the same date as";
else if (d1.before(d2))
relation = "before";
else if (d1.after(d2))
relation = "after";
System.out.println(d1 + " is " + relation + " " + d2);
}
}
DateFormat: setLenient(boolean lenient)
import java.text.DateFormat;
public class Main {
public static boolean isValidDateStr(String date) throws Exception {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
df.setLenient(false);
df.parse(date);
return true;
}
public static void main(String[] args) throws Exception{
System.out.println(isValidDateStr("1900-13-12"));
}
}
DateFormat.SHORT
/*
* Output:
Japan: 4/28/06
Korea: Apr 28, 2006
United Kingdom: 28 April 2006
United States: Friday, April 28, 2006
* */
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class MainClass {
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));
}
}