Java by API/java.text/DateFormatSymbols — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:44, 31 мая 2010
Содержание
DateFormatSymbols: getMonths()
import java.text.DateFormatSymbols;
public class Main {
public static void main(String[] args) {
String[] months = new DateFormatSymbols().getMonths();
for (int i = 0; i < months.length; i++) {
String month = months[i];
System.out.println("month = " + month);
}
}
}
DateFormatSymbols: getShortWeekdays()
/* 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.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
static public void changeWeekDays() {
Date today;
String result;
SimpleDateFormat formatter;
DateFormatSymbols symbols;
String[] defaultDays;
String[] modifiedDays;
symbols = new DateFormatSymbols(new Locale("en", "US"));
defaultDays = symbols.getShortWeekdays();
for (int i = 0; i < defaultDays.length; i++) {
System.out.print(defaultDays[i] + " ");
}
System.out.println();
String[] capitalDays = { "", "SUN", "MON", "TUE", "WED", "THU", "FRI",
"SAT" };
symbols.setShortWeekdays(capitalDays);
modifiedDays = symbols.getShortWeekdays();
for (int i = 0; i < modifiedDays.length; i++) {
System.out.print(modifiedDays[i] + " ");
}
System.out.println();
System.out.println();
formatter = new SimpleDateFormat("E", symbols);
today = new Date();
result = formatter.format(today);
System.out.println(result);
}
static public void main(String[] args) {
changeWeekDays();
}
}
DateFormatSymbols: getWeekdays()
/*
* Output:
dimanche
lundi
mardi
mercredi
jeudi
vendredi
samedi
* */
import java.text.DateFormatSymbols;
import java.util.Locale;
public class MainClass {
public static void main(String args[]) {
DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);
String days[] = symbols.getWeekdays();
for (int i = 0; i < days.length; i++) {
System.out.println(days[i]);
}
}
}
DateFormatSymbols: setEras(String[] newEras)
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
public static void main(String args[]) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat();
DateFormatSymbols dfs = sdf.getDateFormatSymbols();
String era[] = { "BCE", "CE" };
dfs.setEras(era);
sdf.setDateFormatSymbols(dfs);
sdf.applyPattern("MMMM d yyyy G");
System.out.println(sdf.format(new Date()));
}
}
DateFormatSymbols: setShortWeekdays(String[] newShortWeekdays)
/* 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.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
static public void changeWeekDays() {
Date today;
String result;
SimpleDateFormat formatter;
DateFormatSymbols symbols;
String[] defaultDays;
String[] modifiedDays;
symbols = new DateFormatSymbols(new Locale("en", "US"));
defaultDays = symbols.getShortWeekdays();
for (int i = 0; i < defaultDays.length; i++) {
System.out.print(defaultDays[i] + " ");
}
System.out.println();
String[] capitalDays = { "", "SUN", "MON", "TUE", "WED", "THU", "FRI",
"SAT" };
symbols.setShortWeekdays(capitalDays);
modifiedDays = symbols.getShortWeekdays();
for (int i = 0; i < modifiedDays.length; i++) {
System.out.print(modifiedDays[i] + " ");
}
System.out.println();
System.out.println();
formatter = new SimpleDateFormat("E", symbols);
today = new Date();
result = formatter.format(today);
System.out.println(result);
}
static public void main(String[] args) {
changeWeekDays();
}
}
new DateFormatSymbols(Locale loc)
import java.awt.BorderLayout;
import java.text.DateFormatSymbols;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JSpinner Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);
String days[] = symbols.getWeekdays();
SpinnerModel model1 = new SpinnerListModel(days);
SpinnerModel model2 = new SpinnerDateModel();
JSpinner spinner1 = new JSpinner(model1);
JSpinner spinner2 = new JSpinner(model2);
f.add(spinner1, BorderLayout.NORTH);
f.add(spinner2, BorderLayout.SOUTH);
f.setSize(300, 100);
f.setVisible(true);
}
}