Home » Archive

Articles tagged with: javabout.com

Java Codes »

[6 Mar 2009 | No Comment | 418 views]

This example shows how to display all available time zone IDs. These IDs are essential when you want to use classes like Date and Calendar and display the time in another time zone than default.To retrieve the time zones we call the static method getAvailableIDs of the TimeZone class. It returns an array of strings with the ID of each time zone.The array returned is quite extensive and to make it easier to find what we’re looking for in the output, we call the sort method of the Arrays class …

Java Codes »

[6 Mar 2009 | No Comment | 275 views]

To display time in a specific time zone we can use the Calendar class. After it has been created we set the time zone on the instance using the method setTimeZone. It is really all that is to it, after that we can just query the instance for hours and minutes as in the code example below where time in three major cities around the world is displayed.
package com.javadb.examples;import java.util.Calendar;import java.util.TimeZone;/** * * @a*#117;tho*#114; w*#119;*#119;*#46;j*#97;*#118;adb.*#99;om */public class Main {    public void setTimeZones() {                Calendar calNewYork = Calendar.getInstance();        Calendar calParis = Calendar.getInstance();        Calendar calTokyo = Calendar.getInstance();                calNewYork.setTimeZone(TimeZone.getTimeZone(“America/New_York”));        calParis.setTimeZone(TimeZone.getTimeZone(“Europe/Paris”));        calTokyo.setTimeZone(TimeZone.getTimeZone(“Asia/Tokyo”));                System.out.println(“Time in New York: …

Java Codes »

[6 Mar 2009 | No Comment | 325 views]

This example shows how to load a ResourceBundle from the classpath and then enumerate through it.Since ResourceBundle is an abstract class and therefore cannot be instantiated we call its static method getBundle() which returns the ResourceBundle instance.The method has a few overloads but we use the one that takes a basename (in this case ‘Phrases’) and a locale. This means that somewhere on the classpath there has to be a file named ‘Phrases_en_US.properties’.In this example the file only has two rows (see further below). An Enumeration with the keys is …

Java Codes »

[6 Mar 2009 | No Comment | 238 views]

This example shows how to get the currency symbols for different countries. The code below displays the currency symbols for three countries, US, UK and France.
package com.javadb.examples;import java.util.Currency;import java.util.Locale;/** * * *#64;author *#119;*#119;*#119;*#46;java*#100;b.*#99;*#111;*#109;  */public class Main {        public void displayCurrencySymbols() {                Currency currency = Currency.getInstance(Locale.US);         System.out.println(“United States: “ currency.getSymbol());         currency = Currency.getInstance(Locale.UK);        System.out.println(“United Kingdom: “ currency.getSymbol());         currency = Currency.getInstance(Locale.FRANCE);        System.out.println(“France: “ currency.getSymbol());            }     public static void main(String[] args) {        new Main().displayCurrencySymbols();    }  }

The output from the code example:

United States: USDUnited Kingdom: GBPFrance: €

Java Codes »

[6 Mar 2009 | No Comment | 250 views]

This class prints current date and time by creating a Date object and using instances of SimpleDateFormat to format the Date object.See the SimpleDateFormat api reference for more format options.
import java.text.SimpleDateFormat;import java.util.Date;public class GetDateTime {    public GetDateTime() {        SimpleDateFormat sdfDate = new SimpleDateFormat(“dd/MM/yyyy”);    SimpleDateFormat sdfTime = new SimpleDateFormat(“HH:mm:ss”);        Date now = new Date();        String strDate = sdfDate.format(now);    String strTime = sdfTime.format(now);        System.out.println(“Date: “ strDate);    System.out.println(“Time: “ strTime);      }  public static void main(String[] args) {    GetDateTime getDateTime = new GetDateTime();  }} 

Java Codes »

[6 Mar 2009 | No Comment | 395 views]

>This code example shows how to increment and decrement a date using an instance of the Calendar class.The first method, incrementDate, increments the Calendar which is passed to the method by 5 days.The second method, decrementDate, decrements the same Calendar instance with one month.
>import java.util.Calendar;/** * * @*#97;*#117;t*#104;*#111;*#114; *#119;*#119;w*#46;*#106;*#97;v*#97;*#100;*#98;.*#99;*#111;m */public class Main {        public void incrementDate(Calendar cal) {                int daysToIncrement = 5;         cal.add(Calendar.DATE, daysToIncrement);        System.out.println(“Date after increment: “ cal.getTime());    }        public void decrementDate(Calendar cal) {                int monthsToDecrement = -1;        cal.add(Calendar.MONTH, monthsToDecrement);        System.out.println(“Date after decrement: “ cal.getTime());     }    /**     * @param args the command line arguments     */    public static void main(String[] args) {                Calendar …

Java Codes »

[6 Mar 2009 | No Comment | 294 views]

>This code example shows how to output today’s date. It uses the Calendar class. Since it is an abstract class and therefore cannot be instantiated we created it using the getInstance() method. Then we create an instance of the SimpleDateFormat class which takes the format we want to display the date in as argument to the constructor.Finally the method ‘format()’ is called on the SimpleDateFormat object. The format() method takes a java.util.Date object as argument and we can get our hands on one by calling the getTime() method of the …

Java Codes »

[6 Mar 2009 | No Comment | 133 views]

This code shows how to create instances of both the java.util.Date and java.sql.Date classes using a Calendar.
import java.util.Calendar;public class DateUtil {  public void createDates() {    int year = 2006;    int month = 0; //January    int date = 1;        Calendar cal = Calendar.getInstance();        //Clear all fields    cal.clear();        cal.set(Calendar.YEAR, year);    cal.set(Calendar.MONTH, month);    cal.set(Calendar.DATE, date);        //Create instance of java.util.Date    java.util.Date utilDate = cal.getTime();        //Create instance of java.sql.Date    java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis());        System.out.println(utilDate);    System.out.println(sqlDate);  }  public static void main(String[] args) {    DateUtil dateutil = new DateUtil();    dateutil.createDates();  }}

Java Codes »

[6 Mar 2009 | No Comment | 165 views]

Execution in Java always take form of a thread. In simple programs only one thread occurs, often referred to as the ‘main thread’.In some programs though concurrent threads is necessary, or if you are building a Swing application with for example a progress bar you don’t want the main thread to handle the updates of the UI.Then you create an additional thread to take care of that. There are two main ways of creating a thread. The first is to extend the Thread class and the second is to implement …

Java Codes »

[6 Mar 2009 | No Comment | 769 views]

Setting a threads priority can be very useful if one thread has more critical tasks to perform than another.The Thread class has a method called setPriority(int level) with which you can alter the priority a Thread instance has.The priority level range from 1 (least important) to 10 (most important) and if no level is explicitly set, a Thread instance has the priority level of 5.In the first example below no priorites are set, so both threads have the priority level 5. The TestThread class implements the Runnable interface and in …