Please enable JavaScript.
Coggle requires JavaScript to display documents.
Chapter 5. DATES, STRING, AND LOCALIZATION (Working with Dates and Times…
Chapter 5. DATES, STRING, AND LOCALIZATION
Working with Dates and Times
Work with Periods
Convert to a
long
toEpochDay()
LocalDate
Number of days since 01/01/1970
toEpochSecond()
LocalDateTime
and
ZonedDateTime
Number of seconds since 01/01/1970
Create a Period class
Period everyThreeWeeks = Period.ofWeeks(3)
Period everyOtherDay = Period.ofDays(2)
Perioid quaterly = Period.ofMonths(3)
Period everyYearAndWeek = Period.of(1,0,7) // (Year, Month, Day)
Period annually = Period.ofYear(1)
Print out the value
System.out.println((Period.of)1,2,3)); // P1Y2M3D
System.out.println(Period.ofMonths(3)); // P3M
System.out,println(Period.of(0,20,47)) // P20M47D
Work with Durations
Create a Duration
Without
ChronoUnit
Duration hourly = Duration.ofHours(1) //PT1H
Duration everyMinute = Duration.ofMinutes(1) // PT1M
Duration daily = Duration.ofDays(1) //PT24H
Duration everyTenSeconds = Duration.ofSeconds(10) //PT10S
Duration everyMilli = Duration.ofMillis(1) //PT0.001S
Duration everyNano = Duration.ofNanos(1) //PT0.000000001S
With
ChronoUnit
Duration hourly = Duration.of(1, ChronoUnit.HOURS);
Duration everyMinute = Duration.of(1,ChronoUnit.MINUTES);
Duration daily = Duration.of(1, ChronoUnit.DAYS)
Duration everyTenSeconds = Duration.of(10, ChronoUnit.SECONDS);
Duration everyMilli = Duration.of(1, ChronoUnit.MILLIS);
Duration everyNano = Duration.of(1, ChronoUnit.NANOS);
ChronoUnit
for Differences
LocalTime one = LocalTime.of(5,15);
LocalTime two = LocalTime.of(6,30);
System.out.println(ChronoUnit.HOURS.between(one,two));
System.out.println(ChronoUnit.MINITES.between(one,two));
Manipulate Dates and Times
LocalDate
plusMonths / minusMonths
plusYears / minusYears
plusWeeks / minusWeeks
plusDays / minusDays
LocalTime
plusMinutes / minusMinutes
plusSeconds / minusSeconds
plusHours / minusHours
plusNanos / minusNanos
Account for Daylight Savings Time
Create Dates and Times
package
java.time.*
Dates and Times classes
LocalTime
Just a
TIME
+ NO Date + NO Timezone
LocalDateTime
DATE + TIME + NO Timezone
LocalDate
Just a
DATE
+ NO Time + NO Timezone
ZonedDateTime
DATE + TIME + TIMEZONE
Get the current date and time
LocalTime.now()
LocalDateTime.now()
LocalDate.now()
ZonedDateTime.now()
Create new
LocalDate
public static LocalDate
of
(int year, int month, int dayOfMonth)
public static LocalDate
of
(int year, Month month, int dayOfMonth)
LocalTime
public static LocalTime
of
(int hour, int minute)
public static LocalTime
of
(int hour, int minute, int second)
public static LocalTime
of
(int hour, int minute, int second, int nanos)
LocalDateTime
public static LocalDateTime
of
(int year, Month month, int dayOfMonth, int hour, int minute)
public static LocalDateTime
of
(int year, Month month, int dayOfMonth, int hour, int minute, int second)
public static LocalDateTime
of
(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanos)
public static LocalDateTime
of
(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanos)
public static LocalDateTime
of
(int year, int month, int dayOfMonth, int hour, int minute, int second)
public static LocalDateTime
of
(LocalDate date, LocalTime time)
public static LocalDateTime
of
(int year, int month, int dayOfMonth, int hour, int minute)
ZonedDateTime
public static ZonedDateTime
of
(LocalDate date, LocalTime time, ZoneId zone)
public static ZonedDateTime
of
(LocalDateTime dateTime, ZoneId zone)
public static ZonedDateTime
of
(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanos, ZoneId zone)
Working with
Instant
Represent a specific moment in time in the GMT time zone
Instant now = Instant.now();
Instant later = Instant.now();
Duration duration = Duration.between(now, later);
System.out.println(duration.toMillis());
Review the
String
class
Adding Internationalization (i18n) and Localization (l10n)
Using a Resource Bundle
Types
Property file
Specific format with key/value pairs
Language-specific resource bundle
Filename
<Resource bundle name>_<Locale>.properties
Create Resource Bundle
ResourceBundle rb = ResourceBundle.getBundle("Zoo",locale);
Get a set of all keys
Set<String> keys = rb.keySets();
Convert from ResourceBundle to Properties
Properties props = new Properties();
rb.ketSet().stream().forEach(k -> props.put(k, rb.getString(k)));
getProperty("key", "default");
Java class
import java.util.*;
public class Zoo_en extends ListResourceBundle {
protected Object[][] getContents(){
return new Object[][]{
{"hello","Hello"},
{"open","The zoo is open"}
};
}
}
Determine which Resource Bundle to Use
Java class -> Properties file
Drop one thing at a time if there are no matches. First drop the country, and then the language
Look at the default locale and the default resource bundle last
Formatting Numbers
NumberFormat
for Numbers and Currency
Currency
NumberFormat.get
Currency
Instance()
NumberFormat.get
Currency
Instance(locale)
Percentage
NumberFormat.get
Percent
Instance()
NumberFormat.get
Percent
Instance(locale)
Number
NumberFormat.get
Number
Instance()
NumberFormat.get
Number
Instance(locale)
Rounds decimal values
NumberFormat.get
Integer
Instance()
NumberFormat.get
Integer
Instance(locale)
General
NumberFormat.getInstance()
NumberFormat.getInstance(locale)
Number -> String
format()
String -> Number
parse()
Picking a Locale
Create a locale
Get current locale
Locale locale = Locale.getDefault();
System.out.println(locale);
Use constants
System.out.println(Locale.GERMAN);
System.out.println(Locale.GERMANY);
Use constructor
System.out.println(new Locale("fr"));
System.out.println(new Locale("hi", "IN"));
Use Locale.Builder
Locale l1 = new Locale.Builder()
.setRegion("US")
.setLanguage("en)
.build();
Output format
Language only
fr
Language_Country
en_US
Formatting Dates and Times
DateTimeFormatter
ofLocalizedDate
ofLocalizedDateTime
ofLocalizedTime
ofPattern