Skip to content

Commit 2cdefb0

Browse files
author
turingfly
committed
Dates, Strings and Localization
1 parent 081819c commit 2cdefb0

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package datesStringsLocalization;
2+
3+
import java.time.*;
4+
import java.time.temporal.ChronoUnit;
5+
6+
/**
7+
*
8+
* @author chengfeili
9+
* Jun 9, 2017 2:24:48 PM
10+
*
11+
*/
12+
public class DatesAndTimes {
13+
14+
public void create() {
15+
LocalDate date1 = LocalDate.of(2015, Month.JANUARY, 20);
16+
LocalDate date2 = LocalDate.of(2015, 1, 20);
17+
Month month = Month.JANUARY;
18+
// boolean b1 = month == 1; // DOES NOT COMPILE
19+
boolean b2 = month == Month.APRIL; // false
20+
21+
LocalTime time1 = LocalTime.of(6, 15);
22+
LocalTime time2 = LocalTime.of(6, 15, 30);
23+
LocalTime time3 = LocalTime.of(6, 15, 30, 200);
24+
25+
LocalDateTime dateTime1 = LocalDateTime.of(2015, Month.JANUARY, 20, 6, 15, 30);
26+
LocalDateTime dateTime2 = LocalDateTime.of(date1, time1);
27+
ZoneId zone = ZoneId.of("US/Eastern");
28+
ZonedDateTime zoned1 = ZonedDateTime.of(2015, 1, 20, 6, 15, 30, 200, zone);
29+
ZonedDateTime zoned2 = ZonedDateTime.of(date1, time1, zone);
30+
ZonedDateTime zoned3 = ZonedDateTime.of(dateTime1, zone);
31+
}
32+
33+
public void manipulate() {
34+
// Date
35+
LocalDate date = LocalDate.of(2014, Month.JANUARY, 20);
36+
date = date.plusDays(2);
37+
date = date.plusWeeks(1);
38+
date = date.plusMonths(1);
39+
date = date.plusYears(5);
40+
// Date Time
41+
LocalDate date1 = LocalDate.of(2020, Month.JANUARY, 20);
42+
LocalTime time = LocalTime.of(5, 15);
43+
LocalDateTime dateTime = LocalDateTime.of(date1, time).minusDays(1).minusHours(10).minusSeconds(30);
44+
System.out.println(dateTime); // 2020-01-18T19:14:30
45+
}
46+
47+
public void period() {
48+
Period annually = Period.ofYears(1);
49+
Period quarterly = Period.ofMonths(3);
50+
Period everyThreeWeeks = Period.ofWeeks(3);
51+
Period everyOtherDay = Period.ofDays(2);
52+
Period everyYearAndAWeek = Period.of(1, 0, 7);
53+
LocalDate date = LocalDate.of(2014, Month.JANUARY, 20);
54+
date = date.plus(annually);
55+
System.out.println(date);
56+
System.out.println(Period.of(0, 20, 47)); // P20M47D
57+
System.out.println(Period.of(2, 20, 47)); // P2Y20M47D
58+
}
59+
60+
// For Duration, you can specify the number of days, hours, minutes,
61+
// seconds, or nanoseconds.
62+
public void duration() {
63+
Duration daily = Duration.of(1, ChronoUnit.DAYS);
64+
Duration hourly = Duration.of(1, ChronoUnit.HOURS);
65+
Duration everyMinute = Duration.of(1, ChronoUnit.MINUTES);
66+
Duration everyTenSeconds = Duration.of(10, ChronoUnit.SECONDS);
67+
Duration everyMilli = Duration.of(1, ChronoUnit.MILLIS);
68+
Duration everyNano = Duration.of(1, ChronoUnit.NANOS);
69+
70+
LocalDate date = LocalDate.of(2015, 5, 25);
71+
Period period = Period.ofDays(1);
72+
Duration days = Duration.ofDays(1);
73+
System.out.println(date.plus(period)); // 2015–05–26
74+
// System.out.println(date.plus(days)); // Unsupported unit: Seconds
75+
}
76+
77+
// The Instant class represents a specific moment in time in the GMT time
78+
// zone.
79+
public void instants() {
80+
Instant now = Instant.now(); // do something time consuming Instant
81+
Instant later = Instant.now();
82+
Duration duration = Duration.between(now, later);
83+
System.out.println(duration.toMillis());
84+
85+
LocalDate date = LocalDate.of(2015, 5, 25);
86+
LocalTime time = LocalTime.of(11, 55, 00);
87+
ZoneId zone = ZoneId.of("US/Eastern");
88+
ZonedDateTime zonedDateTime = ZonedDateTime.of(date, time, zone);
89+
Instant instant = zonedDateTime.toInstant();
90+
91+
Instant nextDay = instant.plus(1, ChronoUnit.DAYS);
92+
System.out.println(nextDay);
93+
Instant nextHour = instant.plus(1, ChronoUnit.HOURS);
94+
System.out.println(nextHour);
95+
// Instant nextWeek = instant.plus(1, ChronoUnit.WEEKS); // exception
96+
}
97+
98+
public static void main(String[] args) {
99+
DatesAndTimes dt = new DatesAndTimes();
100+
dt.create();
101+
dt.manipulate();
102+
dt.period();
103+
dt.duration();
104+
dt.instants();
105+
}
106+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package datesStringsLocalization;
2+
3+
import java.time.LocalDate;
4+
import java.time.LocalDateTime;
5+
import java.time.LocalTime;
6+
import java.time.Month;
7+
import java.time.format.DateTimeFormatter;
8+
import java.time.format.FormatStyle;
9+
10+
/**
11+
*
12+
* @author chengfeili
13+
* Jun 9, 2017 3:57:38 PM
14+
*
15+
*/
16+
public class FormattingDatesAndTimes {
17+
18+
public void test() {
19+
LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
20+
LocalTime time = LocalTime.of(11, 12, 34);
21+
LocalDateTime dateTime = LocalDateTime.of(date, time);
22+
System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE)); // 2020-01-20
23+
System.out.println(time.format(DateTimeFormatter.ISO_LOCAL_TIME)); // 11:12:34
24+
System.out.println(dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); // 2020-01-20T11:12:34
25+
26+
DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
27+
System.out.println(shortDateTime.format(dateTime)); // 1/20/20
28+
System.out.println(shortDateTime.format(date)); // 1/20/20
29+
// System.out.println(shortDateTime.format(time)); //
30+
// UnsupportedTemporalTypeException
31+
32+
// short and medium
33+
DateTimeFormatter shortF = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
34+
DateTimeFormatter mediumF = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
35+
System.out.println(shortF.format(dateTime)); // 1/20/20 11:12 AM
36+
System.out.println(mediumF.format(dateTime)); // Jan 20, 2020 11:12:34AM
37+
38+
// Customize your own formatter
39+
DateTimeFormatter f = DateTimeFormatter.ofPattern("MMMM dd, yyyy, hh:mm");
40+
System.out.println(dateTime.format(f)); // January 20, 2020, 11:12
41+
42+
// String to Datetime
43+
DateTimeFormatter fm = DateTimeFormatter.ofPattern("MM dd yyyy");
44+
LocalDate date1 = LocalDate.parse("01 02 2015", fm);
45+
LocalTime time1 = LocalTime.parse("11:22");
46+
System.out.println(date1); // 2017–06–09
47+
System.out.println(time1); // 11:22
48+
}
49+
50+
public static void main(String[] args) {
51+
FormattingDatesAndTimes fdt = new FormattingDatesAndTimes();
52+
fdt.test();
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package datesStringsLocalization;
2+
3+
import java.text.NumberFormat;
4+
import java.text.ParseException;
5+
import java.util.Locale;
6+
7+
/**
8+
*
9+
* @author chengfeili
10+
* Jun 9, 2017 3:49:38 PM
11+
*
12+
* Once you have the NumberFormat instance, you can call format() to
13+
* turn a number into a String and parse() to turn a String into a
14+
* number.
15+
*
16+
* The format classes are not thread-safe. Do not store them in instance
17+
* variables or static variables.
18+
*/
19+
public class FormattingNumbers {
20+
21+
public void test() throws ParseException {
22+
int attendeesPerYear = 3_200_000;
23+
int attendeesPerMonth = attendeesPerYear / 12;
24+
NumberFormat us = NumberFormat.getInstance(Locale.US);
25+
System.out.println(us.format(attendeesPerMonth)); // 266,666
26+
NumberFormat g = NumberFormat.getInstance(Locale.GERMANY);
27+
System.out.println(g.format(attendeesPerMonth)); // 266.666
28+
NumberFormat ca = NumberFormat.getInstance(Locale.CANADA_FRENCH);
29+
System.out.println(ca.format(attendeesPerMonth)); // 266 666
30+
31+
double price = 48;
32+
NumberFormat usa = NumberFormat.getCurrencyInstance();
33+
System.out.println(usa.format(price)); // $48.00
34+
35+
NumberFormat en = NumberFormat.getInstance(Locale.US);
36+
NumberFormat fr = NumberFormat.getInstance(Locale.FRANCE);
37+
String s = "40.45";
38+
System.out.println(en.parse(s)); // 40.45
39+
System.out.println(fr.parse(s)); // 40
40+
41+
NumberFormat nf = NumberFormat.getInstance();
42+
String one = "456abc";
43+
String two = "-2.5165x10";
44+
String three = "x85.3";
45+
System.out.println(nf.parse(one));
46+
System.out.println(nf.parse(two));
47+
// System.out.println(nf.parse(three)); // throws ParseException
48+
}
49+
50+
public static void main(String[] args) throws ParseException {
51+
FormattingNumbers fm = new FormattingNumbers();
52+
fm.test();
53+
}
54+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package datesStringsLocalization;
2+
3+
import java.util.*;
4+
5+
/**
6+
*
7+
* @author chengfeili
8+
* Jun 9, 2017 2:56:03 PM
9+
*
10+
* Internationalization is the process of designing your program so it
11+
* can be adapted.
12+
* Localization means actually supporting multiple locales.
13+
*/
14+
public class InternationalizationLocalization {
15+
16+
public void locale() {
17+
Locale locale = Locale.getDefault();
18+
System.out.println(locale); // en_US
19+
Locale l1 = new Locale.Builder().setLanguage("en").setRegion("US").build();
20+
Locale l2 = new Locale.Builder().setRegion("US").setLanguage("en").build();
21+
}
22+
23+
public static void main(String[] args) {
24+
InternationalizationLocalization ii = new InternationalizationLocalization();
25+
ii.locale();
26+
}
27+
}

0 commit comments

Comments
 (0)