GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Formatting — java.evolved

Home / Topics / Formatting
🏷 Formatting 4 patterns

Topic: Formatting

All Java patterns related to Formatting — java.evolved

Strings

String.indent() and transform()

Old String[] lines = text.split("\n"); StringBuilder sb = new StringBuilder(); for (String line : lines) { sb.append(" ").append(line) .append("\n"); } String indented = sb.toString();
Modern String indented = text.indent(4); String result = text .transform(String::strip) .transform(s -> s.replace(" ", "-"));
hover to see modern →
JDK 12+ learn more →
Strings

String.formatted()

Old String msg = String.format( "Hello %s, you are %d", name, age );
Modern String msg = "Hello %s, you are %d" .formatted(name, age);
hover to see modern →
JDK 15+ learn more →
Date/Time

Date formatting

Old // Not thread-safe! SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String formatted = sdf.format(date); // Must synchronize for concurrent use
Modern DateTimeFormatter fmt = DateTimeFormatter.ofPattern( "uuuu-MM-dd"); String formatted = LocalDate.now().format(fmt); // Thread-safe, immutable
hover to see modern →
JDK 8+ learn more →
Date/Time

HexFormat

Old // Pad to 2 digits, uppercase String hex = String.format( "%02X", byteValue); // Parse hex string int val = Integer.parseInt( "FF", 16);
Modern var hex = HexFormat.of() .withUpperCase(); String s = hex.toHexDigits( byteValue); byte[] bytes = hex.parseHex("48656C6C6F");
hover to see modern →
JDK 17+ learn more →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.