public class Test { public Test() { } public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMMM dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR,2002); cal.set(Calendar.MONTH,Calendar.AUGUST); cal.set(Calendar.DAY_OF_MONTH,31); System.out.println(" Initialy set date: " + sdf.format(cal.getTime())); cal.set(Calendar.MONTH,Calendar.SEPTEMBER); System.out.println(" Date with month changed : " + sdf.format(cal.getTime())); cal.set(Calendar.DAY_OF_MONTH,30); System.out.println(" Date with day changed : " + sdf.format(cal.getTime())); } } |
Пример 14.1. |
Закрыть окно |
Initialy set date: 2002 August 31 22:57:47 Date with month changed : 2002 October 01 22:57:47 Date with day changed : 2002 October 30 22:57:47 |
Пример 14.2. |
Закрыть окно |
public class Test { public Test() { } public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMMM dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR,2002); cal.set(Calendar.MONTH,Calendar.AUGUST); cal.set(Calendar.DAY_OF_MONTH,31); System.out.println(" Initialy set date: " + sdf.format(cal.getTime())); cal.set(Calendar.MONTH,Calendar.SEPTEMBER); cal.set(Calendar.DAY_OF_MONTH,30); System.out.println(" Date with day and month changed : " + sdf.format(cal.getTime())); } } |
Пример 14.3. |
Закрыть окно |
Initialy set date: 2002 August 31 23:03:51 Date with day and month changed: 2002 September 30 23:03:51 |
Пример 14.4. |
Закрыть окно |
public class Test { public Test() { } public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMMM dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR,2002); cal.set(Calendar.MONTH,Calendar.AUGUST); cal.set(Calendar.DAY_OF_MONTH,31); cal.set(Calendar.HOUR_OF_DAY,19); cal.set(Calendar.MINUTE,30); cal.set(Calendar.SECOND,00); System.out.println("Current date: " + sdf.format(cal.getTime())); cal.add(Calendar.SECOND,75); System.out.println("Current date: " + sdf.format(cal.getTime())); cal.add(Calendar.MONTH,1); System.out.println("Current date: " + sdf.format(cal.getTime())); } } |
Пример 14.5. |
Закрыть окно |
Current date: 2002 August 31 19:30:00 Rule 1: 2002 August 31 19:31:15 Rule 2: 2002 September 30 19:31:15 |
Пример 14.6. |
Закрыть окно |
public class Test { public Test() { } public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMMM dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR,2002); cal.set(Calendar.MONTH,Calendar.AUGUST); cal.set(Calendar.DAY_OF_MONTH,31); cal.set(Calendar.HOUR_OF_DAY,19); cal.set(Calendar.MINUTE,30); cal.set(Calendar.SECOND,00); System.out.println("Current date: " + sdf.format(cal.getTime())); cal.roll(Calendar.SECOND,75); System.out.println("Rule 1: " + sdf.format(cal.getTime())); cal.roll(Calendar.MONTH,1); System.out.println("Rule 2: " + sdf.format(cal.getTime())); } } |
Пример 14.7. |
Закрыть окно |
Current date: 2002 August 31 19:30:00 Rule 1: 2002 August 31 19:30:15 Rule 2: 2002 September 30 19:30:15 |
Пример 14.8. |
Закрыть окно |
public class Test { public Test() { } public static void main(String[] args) { Test test = new Test(); TimeZone tz = TimeZone.getDefault(); int rawOffset = tz.getRawOffset(); System.out.println("Current TimeZone" + tz.getDisplayName() + tz.getID() + "\n\n"); // Dispaly all available TimeZones System.out.println("All Available TimeZones \n"); String[] idArr = tz.getAvailableIDs(); for(int cnt=0;cnt < idArr.length;cnt++){ tz = TimeZone.getTimeZone(idArr[cnt]); System.out.println(test.padr(tz.getDisplayName() + tz.getID(),64) + " raw offset=" + tz.getRawOffset() + ";hour offset=(" + tz.getRawOffset()/ (1000 * 60 * 60 ) + ")"); } // Dispaly all available TimeZones same as for Moscow System.out.println("\n\n TimeZones same as for Moscow \n"); idArr = tz.getAvailableIDs(rawOffset); for(int cnt=0;cnt < idArr.length;cnt++){ tz = TimeZone.getTimeZone(idArr[cnt]); System.out.println(test.padr(tz.getDisplayName()+ tz.getID(),64) + " raw offset=" + tz.getRawOffset() + ";hour offset=(" + tz.getRawOffset()/ (1000 * 60 * 60 ) + ")"); } } String padr(String str,int len){ if(len - str.length() > 0){ char[] buf = new char[len - str.length()]; Arrays.fill(buf,' '); return str + new String(buf); }else{ return str.substring(0,len); } } } |
Пример 14.9. |
Закрыть окно |
Current TimeZone Moscow Standard TimeEurope/ Moscow TimeZones same as for Moscow Eastern African TimeAfrica/Addis_Aba raw offset=10800000;hour offset=(3) Eastern African TimeAfrica/Asmera raw offset=10800000;hour offset=(3) Eastern African TimeAfrica/Dar_es_Sa raw offset=10800000;hour offset=(3) Eastern African TimeAfrica/Djibouti raw offset=10800000;hour offset=(3) Eastern African TimeAfrica/Kampala raw offset=10800000;hour offset=(3) Eastern African TimeAfrica/Khartoum raw offset=10800000;hour offset=(3) Eastern African TimeAfrica/Mogadishu raw offset=10800000;hour offset=(3) Eastern African TimeAfrica/Nairobi raw offset=10800000;hour offset=(3) Arabia Standard TimeAsia/Aden raw offset=10800000;hour offset=(3) Arabia Standard TimeAsia/Baghdad raw offset=10800000;hour offset=(3) Arabia Standard TimeAsia/Bahrain raw offset=10800000;hour offset=(3) Arabia Standard TimeAsia/Kuwait raw offset=10800000;hour offset=(3) Arabia Standard TimeAsia/Qatar raw offset=10800000;hour offset=(3) Arabia Standard TimeAsia/Riyadh raw offset=10800000;hour offset=(3) Eastern African TimeEAT raw offset=10800000;hour offset=(3) Moscow Standard TimeEurope/Moscow raw offset=10800000;hour offset=(3) Eastern African TimeIndian/Antananar raw offset=10800000;hour offset=(3) Eastern African TimeIndian/Comoro raw offset=10800000;hour offset=(3) Eastern African TimeIndian/Mayotte raw offset=10800000;hour offset=(3) |
Пример 14.10. |
Закрыть окно |
public class Test { public Test() { } public static void main(String[] args) { Test test = new Test(); SimpleTimeZone stz = new SimpleTimeZone( TimeZone.getDefault().getRawOffset() ,TimeZone.getDefault().getID() ,Calendar.MARCH ,-1 ,Calendar.SUNDAY ,test.getTime(2,0,0,0) ,Calendar.OCTOBER ,-1 ,Calendar.SUNDAY ,test.getTime(3,0,0,0) ); System.out.println(stz.toString()); } int getTime(int hour,int min,int sec,int ms){ return hour * 3600000 + min * 60000 + sec * 1000 + ms; } } |
Пример 14.11. |
Закрыть окно |
java.util.SimpleTimeZone[id=Europe/Moscow,offset=10800000,dstSavings=3600000,useDaylight=true, startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=7200000,startTimeMode=0, endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=10800000,endTimeMode=0] |
Пример 14.12. |
Закрыть окно |
public class TestObservable extends java.util.Observable { private String name = ""; public TestObservable(String name) { this.name = name; } public void modify() { setChanged(); } public String getName() { return name; } } public class TestObserver implements java.util.Observer { private String name = ""; public TestObserver(String name) { this.name = name; } public void update(java.util.Observable o,Object arg) { String str = "Called update of " + name; str += " from " + ((TestObservable)o).getName(); str += " with argument " + (String)arg; System.out.println(str); } } public class Test { public Test() { } public static void main(String[] args) { Test test = new Test(); TestObservable to = new TestObservable("Observable"); TestObserver o1 = new TestObserver("Observer 1"); TestObserver o2 = new TestObserver("Observer 2"); to.addObserver(o1); to.addObserver(o2); to.modify(); to.notifyObservers("Notify argument"); } } |
Пример 14.13. |
Закрыть окно |
Called update of Observer 2 from Observable with argument Notify argument Called update of Observer 1 from Observable with argument Notify argument |
Пример 14.14. |
Закрыть окно |
List l = Collections.synchronizedList(new ArrayList(...)); public class Test { public Test() { } public static void main(String[] args) { Test t = new Test(); ArrayList al = new ArrayList(); al.add("First element"); al.add("Second element"); al.add("Third element"); Iterator it = al.iterator(); while(it.hasNext()) { System.out.println((String)it.next()); } System.out.println("\n"); al.add(2,"Insertion"); it = al.iterator(); while(it.hasNext()){ System.out.println((String)it.next()); } } } |
Пример 14.15. |
Закрыть окно |
First element Second element Third element Firts element Second element Insertion Third element |
Пример 14.16. |
Закрыть окно |
public class Test { public Test() { } public static void main(String[] args) { Test test = new Test(); LinkedList ll = new LinkedList(); ll.add("Element1"); ll.addFirst("Element2"); ll.addFirst("Element3"); ll.addLast("Element4"); test.dumpList(ll); ll.remove(2); test.dumpList(ll); String element = (String)ll.getLast(); ll.remove(element); test.dumpList(ll); } private void dumpList(List list){ Iterator it = list.iterator(); System.out.println(); while(it.hasNext()){ System.out.println((String)it.next()); } } } |
Пример 14.17. |
Закрыть окно |
Element3 Element2 Element1 Element4 Element3 Element2 Element4 Element3 Element2 |
Пример 14.18. |
Закрыть окно |
public class Test { private class TestObject{ String text = ""; public TestObject(String text){ this.text = text; }; public String getText(){ return this.text; } public void setText(String text){ this.text = text; } } public Test() { } public static void main(String[] args) { Test t = new Test(); TestObject to = null; HashMap hm = new HashMap(); hm.put("Key1",t.new TestObject("Value 1")); hm.put("Key2",t.new TestObject("Value 2")); hm.put("Key3",t.new TestObject("Value 3")); to = (TestObject)hm.get("Key1"); System.out.println("Object value for Key1 = " + to.getText() + "\n"); System.out.println("Iteration over entrySet"); Map.Entry entry = null; Iterator it = hm.entrySet().iterator(); // Итератор для перебора всех точек входа в Map while(it.hasNext()){ entry = (Map.Entry)it.next(); System.out.println("For key = " + entry.getKey() + " value = " + ((TestObject)entry.getValue()).getText()); } System.out.println(); System.out.println("Iteration over keySet"); String key = ""; // Итератор для перебора всех ключей в Map it = hm.keySet().iterator(); while(it.hasNext()){ key = (String)it.next(); System.out.println( "For key = " + key + " value = " + ((TestObject)hm.get(key)).getText()); } } } |
Пример 14.19. |
Закрыть окно |
Object value for Key1 = Value 1 Iteration over entrySet For key = Key3 value = Value 3 For key = Key2 value = Value 2 For key = Key1 value = Value 1 Iteration over keySet For key = Key3 value = Value 3 For key = Key2 value = Value 2 For key = Key1 value = Value 1 |
Пример 14.20. |
Закрыть окно |
public class Test { private class TestObject { private String name = ""; public TestObject(String name) { this.name = name; } } private class MyComparator implements Comparator { public int compare(Object l,Object r) { String left = (String)l; String right = (String)r; return -1 * left.compareTo(right); } } public Test() { } public static void main(String[] args) { Test test = new Test(); Vector v = new Vector(); v.add("bbbbb"); v.add("aaaaa"); v.add("ccccc"); System.out.println("Default elements order"); test.dumpList(v); Collections.sort(v); System.out.println("Default sorting order"); test.dumpList(v); System.out.println("Reverse sorting order with providing imlicit comparator"); Collections.sort(v,test.new MyComparator()); test.dumpList(v); } private void dumpList(List l) { Iterator it = l.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } } |
Пример 14.21. |
Закрыть окно |
public class Test { public Test() { } public static void main(String[] args) { Test test = new Test(); Properties props = new Properties(); StringWriter sw = new StringWriter(); sw.write("Key1 = Vlaue1 \n"); sw.write(" Key2 : Vlaue2 \r\n"); sw.write(" Key3 Vlaue3 \n "); InputStream is = new ByteArrayInputStream(sw.toString().getBytes()); try { props.load(is); } catch (IOException ex) { ex.printStackTrace(); } props.list(System.out); props.setProperty("Key1","Modified Value1"); props.setProperty("Key4","Added Value4"); props.list(System.out); } } |
Пример 14.22. |
Закрыть окно |
-- listing properties -- Key3=Vlaue3 Key2=Vlaue2 Key1=Vlaue1 -- listing properties -- Key4=Added Value4 Key3=Vlaue3 Key2=Vlaue2 Key1=Modified Value1 |
Пример 14.23. |
Закрыть окно |
public class Test { public Test() { } public static void main(String[] args) { Test test = new Test(); Random r = new Random(100); // Generating the same sequence numbers for(int cnt=0;cnt<9;cnt++){ System.out.print(r.nextInt() + " "); } System.out.println(); r = new Random(100); for(int cnt=0;cnt<9;cnt++) { System.out.print(r.nextInt() + " "); } System.out.println(); // Generating sequence of bytes byte[] randArray = new byte[8]; r.nextBytes(randArray); test.dumpArray(randArray); } void dumpArray(byte[] arr) { for(int cnt=0;cnt< arr.length;cnt++) { System.out.print(arr[cnt]); } System.out.println(); } } |
Пример 14.24. |
Закрыть окно |
-1193959466 - 1139614796 837415749 -1220615319 -1429538713 118249332 -951589224 -1193959466 -1139614796 837415749 -1220615319 -1429538713 118249332 -951589224 81;-6;-107;77;118;17;93; -98; |
Пример 14.25. |
Закрыть окно |
public class Test { public Test() { } public static void main(String[] args) { Test test = new Test(); Locale l = Locale.getDefault(); System.out.println(l.getCountry() + " " + l.getDisplayCountry() + " " + l.getISO3Country()); System.out.println(l.getLanguage() + " " + l.getDisplayLanguage() + " " + l.getISO3Language()); System.out.println(l.getVariant() + " " + l.getDisplayVariant()); l = new Locale("ru","RU","WINDOWS"); System.out.println(l.getCountry() + " " + l.getDisplayCountry() + " " + l.getISO3Country()); System.out.println(l.getLanguage() + " " + l.getDisplayLanguage() + " " + l.getISO3Language()); System.out.println(l.getVariant() + " " + l.getDisplayVariant()); } } |
Пример 14.26. |
Закрыть окно |
US United States USA en English eng RU Russia RUS ru Russian rus WINDOWS WINDOWS |
Пример 14.27. |
Закрыть окно |
baseclass + "_" + language1 + "_" + country1 + "_" + variant1 baseclass + "_" + language1 + "_" + country1 + "_" + variant1 + ".properties" baseclass + "_" + language1 + "_" + country1 baseclass + "_" + language1 + "_" + country1 + ".properties" baseclass + "_" + language1 baseclass + "_" + language1 + ".properties" baseclass + "_" + language2 + "_" + country2 + "_" + variant2 baseclass + "_" + language2 + "_" + country2 + "_" + variant2 + ".properties" baseclass + "_" + language2 + "_" + country2 baseclass + "_" + language2 + "_" + country2 + ".properties" baseclass + "_" + language2 baseclass + "_" + language2 + ".properties" baseclass baseclass + ".properties" |
Пример 14.28. |
Закрыть окно |
public class MyResource extends ResourceBundle { private Hashtable res = null; public MyResource() { res = new Hashtable(); res.put("TestKey","English Variant"); } public Enumeration getKeys() { return res.keys(); } protected Object handleGetObject(String key) throws java.util.MissingResourceException { return res.get(key); } } public class MyResource_ru_RU extends ResourceBundle { private Hashtable res = null; public MyResource_ru_RU() { res = new Hashtable(); res.put("TestKey","Русский варинат"); } public Enumeration getKeys() { return res.keys(); } protected Object handleGetObject(String key) throws java.util.MissingResourceException { return res.get(key); } } public class Test { public Test() { } public static void main(String[] args) { Test test = new Test(); ResourceBundle rb = ResourceBundle.getBundle("experiment.MyResource",Locale.getDefault()); System.out.println(rb.getString("TestKey")); rb = ResourceBundle.getBundle("experiment.MyResource", new Locale("ru","RU")); System.out.println(rb.getString("TestKey")); } } |
Пример 14.29. |
Закрыть окно |
public interface Behavior { public String getBehavior(); public String getCapital(); } public class EnglishBehavior implements Behavior{ public EnglishBehavior() { } public String getBehavior(){ return "English behavior"; } public String getCapital(){ return "London"; } } public class RussianBehavior implements Behavior { public RussianBehavior() { } public String getBehavior(){ return "Русский вариант поведения"; } public String getCapital(){ return "Москва"; } } public class MyResourceBundle_ru_RU extends ResourceBundle { Hashtable bundle = null; public MyResourceBundle_ru_RU() { bundle = new Hashtable(); bundle.put("Bundle description","Набор ресурсов для русской локали"); bundle.put("Behavior",new RussianBehavior()); } public Enumeration getKeys() { return bundle.keys(); } protected Object handleGetObject(String key) throws java.util.MissingResourceException { return bundle.get("key"); } } public class MyResourceBundle_en_EN { Hashtable bundle = null; public MyResourceBundle_en_EN() { bundle = new Hashtable(); bundle.put("Bundle description","English resource set"); bundle.put("Behavior",new EnglishBehavior()); } public Enumeration getKeys() { return bundle.keys(); } protected Object handleGetObject(String key) throws java.util.MissingResourceException { return bundle.get("key"); } } public class MyResourceBundle extends ResourceBundle { Hashtable bundle = null; public MyResourceBundle() { bundle = new Hashtable(); bundle.put("Bundle description","Default resource bundle"); bundle.put("Behavior",new EnglishBehavior()); } public Enumeration getKeys() { return bundle.keys(); } protected Object handleGetObject(String key) throws java.util.MissingResourceException { return bundle.get(key); } } public class Using { public Using() { } public static void main(String[] args) { Using u = new Using(); ResourceBundle rb = ResourceBundle.getBundle("lecture.MyResourceBundle", Locale.getDefault()); System.out.println((String)rb.getObject("Bundle description")); Behavior be = (Behavior)rb.getObject("Behavior"); System.out.println(be.getBehavior()); System.out.println(be.getCapital()); rb = ResourceBundle.getBundle("lecture.MyResourceBundle", new Locale("en","EN")); System.out.println((String)rb.getObject("Bundle description")); Behavior be = (Behavior)rb.getObject("Behavior"); System.out.println(be.getBehavior()); System.out.println(be.getCapital()); } |
Пример 14.30. |
Закрыть окно |
Русский набор ресурсов Русский вариант поведения Москва English resource bundle English behavior London |
Пример 14.31. |
Закрыть окно |
public class MyResource extends ListResourceBundle { Vector v = new Vector(); Object[][] resources = { {"StringKey","String"}, {"DoubleKey",new Double(0.0)}, {"VectorKey",v}, }; public MyResource() { super(); v.add("Element 1"); v.add("Element 2"); v.add("Element 3"); } protected Object[][] getContents() { return resources; } } public class Test { public Test() { } public static void main(String[] args) { Test test = new Test(); ResourceBundle rb = ResourceBundle.getBundle("experiment.MyResource",Locale.getDefault()); Vector v = (Vector)rb.getObject("VectorKey"); Iterator it = v.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } } |
Пример 14.32. |
Закрыть окно |