Tutorialspoint

Java Internalization - Formatting Time

import java.text.DateFormat;
import java.util.Date;

public class I18NTester {
   public static void main(String[] args) {
   
      DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getTimeInstance(DateFormat.LONG);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getTimeInstance(DateFormat.FULL);

      System.out.println(dateFormat.format(new Date()));
  
  
   }
}

Java Internalization - Formatting Date

import java.text.DateFormat;
import java.util.Date;

public class I18NTester {
   public static void main(String[] args) {
   
      DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateInstance(DateFormat.LONG);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateInstance(DateFormat.FULL);

      System.out.println(dateFormat.format(new Date()));
  
  
   }
}

Java Internalization - DateFormat Class

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class I18NTester {
   public static void main(String[] args) {
      Locale locale = new Locale("da","DK");

      DateFormat dateFormat = DateFormat.getDateInstance();

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);

      System.out.println(dateFormat.format(new Date()));
   }
}

Java Internalization - Grouping Digits

import java.text.DecimalFormat;

public class I18NTester {
   public static void main(String[] args) {
      double number = 121223232473.4567;

      DecimalFormat decimalFormat = new DecimalFormat();

      System.out.println(number);
      System.out.println(decimalFormat.format(number));
   

      decimalFormat.setGroupingSize(4);
      System.out.println(decimalFormat.format(number));
   
   }
}

Java Internalization - DecimalFormatSymbols Class

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class I18NTester {
   public static void main(String[] args) {
      String pattern = "#,###.###";
      double number = 126473.4567;

      DecimalFormat decimalFormat = new DecimalFormat(pattern);

      System.out.println(decimalFormat.format(number));
   

      DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
      decimalFormatSymbols.setDecimalSeparator(';');
      decimalFormatSymbols.setGroupingSeparator(':');

      decimalFormat = new DecimalFormat(pattern, decimalFormatSymbols);

      System.out.println(decimalFormat.format(number));
   }
}

Java Internalization - Locale Specific DecimalFormat

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class I18NTester {
   public static void main(String[] args) {
      String pattern = "###.##";
      double number = 123.45;

      Locale enlocale  = new Locale("en", "US");
      Locale dalocale  = new Locale("da", "DK");

      DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(enlocale);
      decimalFormat.applyPattern(pattern);

      System.out.println(decimalFormat.format(number));
   

      decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(dalocale);
      decimalFormat.applyPattern(pattern);

      System.out.println(decimalFormat.format(number));     
   }
}

Java Internalization - Formatting Patterns

import java.text.DecimalFormat;

public class I18NTester {
   public static void main(String[] args) {
      String pattern = "###.###";
      double number = 123456789.123;

      DecimalFormat numberFormat = new DecimalFormat(pattern);

      System.out.println(number);

      //pattern ###.###
      System.out.println(numberFormat.format(number));

      //pattern ###.#
      numberFormat.applyPattern("###.#");
      System.out.println(numberFormat.format(number));

      //pattern ###,###.##
      numberFormat.applyPattern("###,###.##");
      System.out.println(numberFormat.format(number));

      number = 9.34;

      //pattern 000.###
      numberFormat.applyPattern("000.##");
      System.out.println(numberFormat.format(number));           
   }
}

Java Internalization - DecimalFormat Class

import java.text.DecimalFormat;

public class I18NTester {
   public static void main(String[] args) {
      String pattern = "####,####.##";
      double number = 123456789.123;
      
      DecimalFormat numberFormat = new DecimalFormat(pattern);

      System.out.println(number);
      
      System.out.println(numberFormat.format(number));
   }
}

Java Internalization - Parse Numbers

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class I18NTester {
   public static void main(String[] args) throws ParseException {
      Locale enLocale = new Locale("en", "US");  
      Locale daLocale = new Locale("da", "DK");

      NumberFormat numberFormat = NumberFormat.getInstance(daLocale);

      System.out.println(numberFormat.parse("100,76"));

      numberFormat = NumberFormat.getInstance(enLocale);

      System.out.println(numberFormat.parse("100,76"));
   }
}

Java Internalization - Set Rounding Mode

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;

public class I18NTester {
   public static void main(String[] args) {
      Locale enLocale = new Locale("en", "US");  
 
      NumberFormat numberFormat = NumberFormat.getInstance(enLocale);

      numberFormat.setMinimumFractionDigits(0);
      numberFormat.setMaximumFractionDigits(0);

      System.out.println(numberFormat.format(99.50));

      numberFormat.setRoundingMode(RoundingMode.HALF_DOWN);

      System.out.println(numberFormat.format(99.50));
   }
}

Previous 1 ... 3 4 5 6 7 8 9 ... 1926 Next
Advertisements
Loading...