# java.text

***

**1. Formatting Numbers:**

```java
NumberFormat numberFormat = NumberFormat.getNumberInstance();
System.out.println(numberFormat.format(12345.6789)); // Output: "12,345.68"
```

**2. Parsing Numbers:**

```java
NumberFormat numberFormat = NumberFormat.getNumberInstance();
Number number = numberFormat.parse("12,345.68"); // Number: 12345.68
```

**3. Formatting Dates:**

```java
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.format(new Date())); // Output: "24/03/2023"
```

**4. Parsing Dates:**

```java
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = dateFormat.parse("24/03/2023"); // Date: Fri Mar 24 00:00:00 PST 2023
```

**5. Formatting Currency:**

```java
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
System.out.println(currencyFormat.format(12345.6789)); // Output: "$12,345.68"
```

**6. Parsing Currency:**

```java
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
Number number = currencyFormat.parse("$12,345.68"); // Number: 12345.68
```

**7. Formatting Percentages:**

```java
NumberFormat percentFormat = NumberFormat.getPercentInstance();
System.out.println(percentFormat.format(0.5)); // Output: "50%"
```

**8. Parsing Percentages:**

```java
NumberFormat percentFormat = NumberFormat.getPercentInstance();
Number number = percentFormat.parse("50%"); // Number: 0.5
```

**9. Formatting Messages:**

```java
MessageFormat messageFormat = new MessageFormat("Hello {0}, your balance is {1}");
System.out.println(messageFormat.format(new Object[] { "John", 12345.67 })); // Output: "Hello John, your balance is 12,345.67"
```

**10. Parsing Messages:**

```java
MessageFormat messageFormat = new MessageFormat("Hello {0}, your balance is {1}");
Object[] objects = messageFormat.parseObject("Hello John, your balance is 12,345.67"); // Objects: [John, 12345.67]
```

**11. Creating Collators:**

```java
Collator collator = Collator.getInstance();
System.out.println(collator.compare("ABC", "ABD")); // Output: -1
```

**12. Using Collators for Sorting:**

```java
List<String> strings = new ArrayList<>();
strings.add("ABC");
strings.add("ABD");
strings.add("XYZ");
Collections.sort(strings, Collator.getInstance()); // Sorted strings: [ABC, ABD, XYZ]
```

**13. Creating Decimal Formats:**

```java
DecimalFormat decimalFormat = new DecimalFormat("0.##");
System.out.println(decimalFormat.format(12345.6789)); // Output: "12,345.68"
```

**14. Parsing Decimal Formats:**

```java
DecimalFormat decimalFormat = new DecimalFormat("0.##");
Number number = decimalFormat.parse("12,345.68"); // Number: 12345.68
```

**15. Creating Scientific Formats:**

```java
DecimalFormat scientificFormat = new DecimalFormat("0.00E0");
System.out.println(scientificFormat.format(12345.6789)); // Output: "1.23E4"
```

**16. Parsing Scientific Formats:**

```java
DecimalFormat scientificFormat = new DecimalFormat("0.00E0");
Number number = scientificFormat.parse("1.23E4"); // Number: 12300
```

**17. Creating Number Formats with Currency Symbols:**

```java
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(currencyFormat.format(12345.6789)); // Output: "$12,345.68"
```

**18. Creating Number Formats with Different Locales:**

```java
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.FRANCE);
System.out.println(numberFormat.format(12345.6789)); // Output: "12 345,68"
```

**19. Creating Number Formats with Custom Patterns:**

```java
NumberFormat numberFormat = NumberFormat.getNumberInstance();
numberFormat.applyPattern("###,###.00");
System.out.println(numberFormat.format(12345.6789)); // Output: "12,345.68"
```

**20. Creating Locale-Specific Collators:**

```java
Collator collator = Collator.getInstance(Locale.FRANCE);
System.out.println(collator.compare("é", "è")); // Output: -1
```

**21. Using Break Iterators to Iterate over Text:**

```java
BreakIterator lineBreak = BreakIterator.getLineInstance();
lineBreak.setText("This is a sample text.");
int start = lineBreak.first();
int end = lineBreak.next();
System.out.println(text.substring(start, end)); // Output: "This is a"
```

**22. Using Choice Formats to Format Choice Lists:**

```java
ChoiceFormat choiceFormat = new ChoiceFormat("1#one|2#two|3#three");
System.out.println(choiceFormat.format(2)); // Output: "two"
```

**23. Using Date and Time Formatters to Format Dates and Times:**

```java
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println(dateFormat.format(new Date())); // Output: "Mar 24, 2023"
```

**24. Parsing Dates and Times:**

```java
DateFormat dateFormat = DateFormat.getDateInstance();
Date date = dateFormat.parse("Mar 24, 2023"); // Date: Fri Mar 24 00:00:00 PST 2023
```

**25. Creating and Using Formatters:**

```java
Formatter formatter = new Formatter();
formatter.format("Hello %s, your balance is %f", "John", 12345.67);
System.out.println(formatter.toString()); // Output: "Hello John, your balance is 12,345.67"
```

**26. Using String Concatenation to Format Output:**

```java
String output = "Hello " + "John" + ", your balance is " + 12345.67;
System.out.println(output); // Output: "Hello John, your balance is 12,345.67"
```

**27. Using String.format() to Format Output:**

```java
String output = String.format("Hello %s, your balance is %f", "John", 12345.67);
System.out.println(output); // Output: "Hello John, your balance is 12,345.67"
```

**28. Using printf() to Format Output:**

```java
printf("Hello %s, your balance is %f", "John", 12345.67); // Output: "Hello John, your balance is 12,345.67"
```

**29. Using Loggers to Log Messages:**

```java
Logger logger = Logger.getLogger("my.logger");
logger.info("Hello, world!"); // Logs an informational message to the "my.logger" category
```

**30. Using SimpleDateFormats to Format Dates:**

```java
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.format(new Date())); // Output: "24/03/2023"
```

**31. Using ChoiceFormats to Format Selections:**

```java
ChoiceFormat choiceFormat = new ChoiceFormat("1#one|2#two|3#three");
System.out.println(choiceFormat.format(2)); // Output: "two"
```

**32. Using Collators to Compare Strings:**

```java
Collator collator = Collator.getInstance();
System.out.println(collator.compare("ABC", "ABD")); // Output: -1 (ABC is before ABD)
```

**33. Using DecFormat to Format Numbers:**

```java
DecimalFormat decimalFormat = new DecimalFormat("0.00");
System.out.println(decimalFormat.format(12345.6789)); // Output: "12,345.68"
```

**34. Using MessageFormats to Compose Strings:**

```java
MessageFormat messageFormat = new MessageFormat("Hello {0}, your balance is {1}");
System.out.println(messageFormat.format(new Object[] { "John", 12345.67 })); // Output: "Hello John, your balance is 12,345.67"
```

**35. Using NumberFormats to Format Numbers:**

```java
NumberFormat numberFormat = NumberFormat.getInstance();
System.out.println(numberFormat.format(12345.6789)); // Output: "12,345.68"
```

**36. Using ResourceBundles to Localize Strings:**

```java
ResourceBundle resourceBundle = ResourceBundle.getBundle("my.messages");
System.out.println(resourceBundle.getString("hello")); // Output: "Hello" (in the current locale)
```

**37. Using SimpleDateFormats to Parse Dates:**

```java
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = dateFormat.parse("24/03/2023"); // Date: Fri Mar 24 00:00:00 PST 2023
```

**38. Using StringBuffers to Efficiently Build Strings:**

```java
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("Hello ");
stringBuffer.append("John");
System.out.println(stringBuffer.toString()); // Output: "Hello John"
```

**39. Using StringBuilders to Efficiently Build Strings:**

```java
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Hello ");
stringBuilder.append("John");
System.out.println(stringBuilder.toString()); // Output: "Hello John"
```

**40. Using Symbols to Format Numbers:**

```java
DecimalFormat decimalFormat = new DecimalFormat("##,###.00");
System.out.println(decimalFormat.format(12345.6789)); // Output: "12,345.68"
```

**41. Using TextBlocks to Write Multiline Strings:**

```java
String textBlock = """
Hello
John
""";
System.out.println(textBlock); // Output: "Hello\nJohn"
```

**42. Using Transforms to Convert Objects to Strings:**

```java
NumberFormat numberFormat = NumberFormat.getInstance();
String numberString = numberFormat.format(12345.6789); // numberString: "12,345.68"
```

**43. Using Variables to Store Formatted Values:**

```java
String name = "John";
double balance = 12345.67;
String message = String.format("Hello %s, your balance is %f", name, balance);
System.out.println(message); // Output: "Hello John, your balance is 12,345.67"
```

**44. Using Wrappers to Convert Primitive Data Types:**

```java
Integer number = Integer.valueOf(12345); // number: 12345
```

**45. Using Exceptions to Handle Input Errors:**

```java
try {
  NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
  Number number = currencyFormat.parse("$12,345.68"); // number: 12345.68
} catch (ParseException e) {
  // Handle the parse exception
}
```

**46. Using Regular Expressions to Validate Input:**

```java
String input = "12345.67";
Pattern pattern = Pattern.compile("[0-9]+\\.[0-9]+");
Matcher matcher = pattern.matcher(input);
boolean isValid = matcher.matches(); // isValid: true
```

**47. Using System.out.format() to Write Formatted Output:**

```java
System.out.format("Hello %s, your balance is %f", "John", 12345.67); // Output: "Hello John, your balance is 12,345.67"
```

**48. Using Locale-Specific Formats:**

```java
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(currencyFormat.format(12345.67)); // Output: "$12,345.67"
```

**49. Using Collators to Sort Strings:**

```java
Collator collator = Collator.getInstance();
List<String> strings = new ArrayList<>();
strings.add("ABC");
strings.add("ABD");
strings.add("XYZ");
Collections.sort(strings, collator); // Sorted strings: [ABC, ABD, XYZ]
```

**50. Using MessageDigests to Create Hash Values:**

```java
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] hash = md5.digest("Hello World".getBytes()); // hash: [12, 34, 56, 78, 90, 101, 112, 123, 134, 145, 156, 167, 178, 189, 190, 201]
```
