Skip to content

Commit 51656a4

Browse files
New Examples
1 parent 4606c95 commit 51656a4

File tree

3 files changed

+159
-14
lines changed

3 files changed

+159
-14
lines changed

src/main/java/com/howtodoinjava/core/datetime/LocaleExamples.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.howtodoinjava.core.datetime;
22

3+
import java.math.RoundingMode;
34
import java.text.DateFormat;
45
import java.text.NumberFormat;
6+
import java.text.ParseException;
7+
import java.util.Currency;
58
import java.util.Date;
69
import java.util.Locale;
710

811
public class LocaleExamples {
9-
public static void main(final String[] args) {
12+
public static void main(final String[] args) throws ParseException {
1013

1114
//1 Get Locale Basic Info
1215
Locale enUsLocale = new Locale("EN", "US");
@@ -32,11 +35,43 @@ public static void main(final String[] args) {
3235
NumberFormat nf = NumberFormat.getInstance(enUsLocale);
3336
System.out.println(nf.format(123456789L));
3437

38+
//Specifying Language Range
39+
Locale.LanguageRange lr = new Locale.LanguageRange("de-*");
40+
3541
//5 Format Currency
36-
NumberFormat cf = NumberFormat.getCurrencyInstance(locale);
42+
/*NumberFormat cf = NumberFormat.getCurrencyInstance(locale);
3743
String currency = cf.format(123.456);
38-
System.out.println(currency);
44+
System.out.println(currency);*/
45+
46+
47+
double amount = 1234.567;
48+
NumberFormat localizedCurrencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
49+
String formattedCurrency = localizedCurrencyFormat.format(amount);
50+
51+
System.out.println(formattedCurrency); // Output: $1,234.57
52+
53+
NumberFormat frCurrencyFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
54+
String frFormattedCurrency = frCurrencyFormat.format(amount);
55+
56+
System.out.println(frFormattedCurrency); // Output: $1,234.57
57+
58+
String currencyString = "$1,234.57";
59+
60+
try {
61+
Number parsedNumber = localizedCurrencyFormat.parse(currencyString);
62+
double parsedAmount = parsedNumber.doubleValue();
63+
System.out.println(parsedAmount); // Output: 1234.57
64+
} catch (ParseException e) {
65+
e.printStackTrace();
66+
}
3967

40-
//6 Decimal Format
68+
try {
69+
NumberFormat roundedCurrencyFormat = NumberFormat.getCurrencyInstance();
70+
roundedCurrencyFormat.setRoundingMode(RoundingMode.HALF_UP);
71+
String formattedValue = roundedCurrencyFormat.format(123456.789d);
72+
System.out.println(formattedValue); // Output: 1234.57
73+
} catch (Exception e) {
74+
e.printStackTrace();
75+
}
4176
}
4277
}
Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,58 @@
11
package com.howtodoinjava.core.datetime;
22

3+
import java.time.LocalDate;
4+
import java.time.LocalDateTime;
5+
import java.time.LocalTime;
36
import java.time.ZoneId;
47
import java.time.ZonedDateTime;
58
import java.time.format.DateTimeFormatter;
69
import java.time.format.FormatStyle;
710
import java.util.Locale;
811

912
public class LocationBasedDateFormatting {
13+
1014
public static void main(final String[] args) {
1115

12-
ZonedDateTime today = ZonedDateTime.now();
13-
14-
DateTimeFormatter format = DateTimeFormatter
15-
.ofLocalizedDateTime(FormatStyle.MEDIUM)
16-
.withLocale(Locale.forLanguageTag("es"))
17-
.withZone(ZoneId.of("UTC"));
18-
//.withLocale(LocaleContextHolder.getLocale());
19-
20-
String formmatedDate = format.format(today);
21-
System.out.println(formmatedDate);
16+
Locale locale = Locale.forLanguageTag("es");
17+
18+
DateTimeFormatter formatter = DateTimeFormatter
19+
.ofLocalizedDateTime(FormatStyle.MEDIUM)
20+
.withLocale(locale);
21+
22+
String formattedDate = formatter.format(ZonedDateTime.now());
23+
System.out.println(formattedDate); // 14 ene 2024, 8:21:54
24+
25+
DateTimeFormatter chFormatter = DateTimeFormatter
26+
.ofLocalizedDateTime(FormatStyle.MEDIUM)
27+
.withLocale(Locale.forLanguageTag("zh"));
28+
29+
formattedDate = chFormatter.format(ZonedDateTime.now());
30+
System.out.println(formattedDate); // 2024年1月14日 08:21:54
31+
32+
Locale.setDefault(Locale.GERMANY);
33+
34+
String ld = LocalDate.now().format(
35+
DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
36+
System.out.println(ld); // 14. Januar 2024
37+
38+
String lt = LocalTime.now().format(
39+
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT));
40+
System.out.println(lt); // 08:14
41+
42+
String ldt = LocalDateTime.now().format(
43+
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
44+
System.out.println(ldt); // 14.01.2024, 08:14:25
45+
46+
// ofLocalizedPattern() examples
47+
48+
Locale.setDefault(Locale.ENGLISH);
49+
50+
String ld1 = LocalDate.now().format(
51+
DateTimeFormatter.ofLocalizedPattern("yMM"));
52+
System.out.println(ld1); // 01.2024
53+
54+
String ld2 = LocalDate.now().format(
55+
DateTimeFormatter.ofLocalizedPattern("yMMM"));
56+
System.out.println(ld2); // 01.2024
2257
}
2358
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.howtodoinjava.core.jmh;
2+
3+
import java.util.concurrent.ThreadLocalRandom;
4+
import java.util.concurrent.TimeUnit;
5+
import org.openjdk.jmh.annotations.Benchmark;
6+
import org.openjdk.jmh.annotations.BenchmarkMode;
7+
import org.openjdk.jmh.annotations.Mode;
8+
import org.openjdk.jmh.annotations.OutputTimeUnit;
9+
import org.openjdk.jmh.annotations.Scope;
10+
import org.openjdk.jmh.annotations.State;
11+
import org.openjdk.jmh.infra.Blackhole;
12+
import org.openjdk.jmh.runner.Runner;
13+
import org.openjdk.jmh.runner.options.Options;
14+
import org.openjdk.jmh.runner.options.OptionsBuilder;
15+
16+
@State(Scope.Thread)
17+
public class IntToStringBenchmark {
18+
19+
private ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
20+
21+
@Benchmark
22+
@BenchmarkMode(Mode.AverageTime)
23+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
24+
public void methodIntegerToString(Blackhole blackhole) {
25+
int i = threadLocalRandom.nextInt(1_00_000, 9_99_999);
26+
String s = Integer.toString(i);
27+
blackhole.consume(s);
28+
}
29+
30+
@Benchmark
31+
@BenchmarkMode(Mode.AverageTime)
32+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
33+
public void dmethodIntegerToStringV2(Blackhole blackhole) {
34+
Integer i = threadLocalRandom.nextInt(1_00_000, 9_99_999);
35+
String s = Integer.toString(i);
36+
blackhole.consume(s);
37+
}
38+
39+
@Benchmark
40+
@BenchmarkMode(Mode.AverageTime)
41+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
42+
public void cmethodStringValueOf(Blackhole blackhole) {
43+
int i = threadLocalRandom.nextInt(1_00_000, 9_99_999);
44+
String s = String.valueOf(i);
45+
blackhole.consume(s);
46+
}
47+
48+
@Benchmark
49+
@BenchmarkMode(Mode.AverageTime)
50+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
51+
public void bmethodStringFormat(Blackhole blackhole) {
52+
int i = threadLocalRandom.nextInt(1_00_000, 9_99_999);
53+
String s = String.format("%d", i);
54+
blackhole.consume(s);
55+
}
56+
57+
@Benchmark
58+
@BenchmarkMode(Mode.AverageTime)
59+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
60+
public void amethodStringTemplate(Blackhole blackhole) {
61+
int i = threadLocalRandom.nextInt(1_00_000, 9_99_999);
62+
String s = STR."\{i}";
63+
blackhole.consume(s);
64+
}
65+
66+
public static void main(String[] args) throws Exception {
67+
68+
Options opt = new OptionsBuilder()
69+
.include(IntToStringBenchmark.class.getSimpleName())
70+
.forks(1)
71+
.build();
72+
73+
new Runner(opt).run();
74+
}
75+
}

0 commit comments

Comments
 (0)