Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
      a) Simple User entity class

 

 

package basics.basicsExamples;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;

/** ****************************************************************************
 * FORMATING EXAMPLE CLASS
 * @author stanislav.zorjan
 */
public class FormatingExample {
    
    
    Locale czLocale = new Locale.Builder().setLanguage("cs").setRegion("CZ").build();
    Locale srbLocale = new Locale.Builder().setLanguage("sr").setRegion("SR").build();
    
    
    /** ************************************************************************
     * Default constructor
     */
    public FormatingExample() {
	
	//stringFormater();
	//numberFormater();
	//currencyFormater();
	//percentFormater();
	dateFormater();
	
    }
    
    /** ************************************************************************
     * Formating strings
     */
    private void stringFormater(){
	
	
	  // %s = String
	  // %d = Digit
	  // %f = Float
	  // %n = New line
	  // %tY = Year
	  // %ty = Year in 2 digit format
	  // %tB = Month name
	  // %tm = Month in 2 digits format
	  // %td = Day of the month in format (02)
	  // %te = Day of the month in format (2)
	  // %tD = Date in format (02/02/1980)
	  // %tl = Hours in 12 hour format
	  // %tM = Minutes 00... 59
	  // %tp = am/pm time
	  // %% = prints single "%"
	  
	  // Flags
	  // %05d = makes digit 5 "characters" in length adding "0" in frnt (00010) 
	  // %10d = adding 10 whitespaces infront of the digit
	  // %.05f = makes 5 decimal places
	  // %+f = displays +/- signs depending on float value
	  
	  // %1$ = argument index

	

	String str0 = "String for %s is to display %d %+.3f %n%tp %tY%n";
	
	System.out.format(czLocale, str0, "jedan", 10, (4000.0F-40000F), Calendar.getInstance(), Calendar.getInstance(), "10000");
	System.out.printf(srbLocale, str0, "jedan", 10, 4000.0F, Calendar.getInstance(), Calendar.getInstance(), "10000");
	
	// formating string using argument index
	System.out.format("%n%1$s %3$s %2$s %n", "string 1", "string 2", "string 3");
	System.out.println("");
	
    }
    
    /** ************************************************************************
     * Formating numbers
     */
    private void numberFormater(){
	
	// formating numbers using decimal pattern formater
	System.out.println(new DecimalFormat("###,###.###").format(123456.789));
	System.out.println(new DecimalFormat("##,##.##").format(1234567891.12345));
	System.out.println("");
	
	// formating numbers based on locale
	System.out.println(DecimalFormat.getInstance(Locale.FRANCE).format(1234567891.12345));
	System.out.println(DecimalFormat.getInstance(czLocale).format(1234567891.12345));
	System.out.println(DecimalFormat.getInstance(srbLocale).format(1234567891.12345));
	System.out.println("");
	
    }
    
    /** ************************************************************************
     * Formating currency
     */
    private void currencyFormater(){
	
	// formating numbers using locale/currency
	System.out.println(DecimalFormat.getCurrencyInstance(czLocale).format(1234567891.12345));
	System.out.println(DecimalFormat.getCurrencyInstance(srbLocale).format(1234567891.12345));
    
    }
    
    /** ************************************************************************
     * Formating percents
     */
    private void percentFormater(){
	
	// formating percents using locale/country
	System.out.println(DecimalFormat.getPercentInstance(Locale.FRANCE).format(1234567891.12345));
	System.out.println(DecimalFormat.getPercentInstance(srbLocale).format(1234567891.12345));
	
    }
    
    /** ************************************************************************
     * Formating date
     */
    private void dateFormater(){
	
	DateFormat format;
	Date date = Calendar.getInstance(srbLocale).getTime();
	
	
	// Formating date in different ways
	
	format = DateFormat.getDateInstance(DateFormat.FULL, srbLocale);
	System.out.println(format.format(date));
	
	format = DateFormat.getDateInstance(DateFormat.LONG, srbLocale);
	System.out.println(format.format(date));
	
	format = DateFormat.getDateInstance(DateFormat.MEDIUM, srbLocale);
	System.out.println(format.format(date));
	
	format = DateFormat.getDateInstance(DateFormat.SHORT, srbLocale);
	System.out.println(format.format(date));
	
	format = new SimpleDateFormat("dd.MM.yyyy");
	System.out.println(format.format(date));
	
	format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z", srbLocale);
	System.out.println(format.format(date));
	
    }
    
}